• json.net处理复杂json


    -------------JSON字符串---------
    {"id":"-1","result":{"relations":[{"ptId":"713990455","game":"风云","gameid":"50","worldid":"1.1","nickname":"贵族≮蓝皓
    ≯","gender":"0","relation":"1"},{"ptId":"714626904","game":"风云","gameid":"50","worldid":"1.1","nickname":"刘凡
    吻","gender":"0","relation":"1"},{"ptId":"719696772","game":"风云","gameid":"50","worldid":"1.1","nickname":"贵族≮杀神
    ≯","gender":"0","relation":"1"},{"ptId":"713969267","game":"风云","gameid":"50","worldid":"1.1","nickname":"吉米
    仔","gender":"0","relation":"1"},{"ptId":"714286741","game":"风云","gameid":"50","worldid":"1.1","nickname":"指定天
    下","gender":"0","relation":"1"},{"ptId":"714905671","game":"风云","gameid":"50","worldid":"1.1","nickname":"冰玉※雅
    儿","gender":"1","relation":"1"},{"ptId":"719695948","game":"风云","gameid":"50","worldid":"1.1","nickname":"ξψ妹妹
    δοο","gender":"1","relation":"1"},{"ptId":"714884563","game":"风云","gameid":"50","worldid":"1.1","nickname":"药王苗子的老
    婆","gender":"0","relation":"1"},{"ptId":"714726500","game":"风云","gameid":"50","worldid":"1.1","nickname":"贵族≮Kiss
    ≯","gender":"0","relation":"32"},{"ptId":"866101129","game":"风云","gameid":"50","worldid":"1.1","nickname":"聂
    风","gender":"0","relation":"32"}],"friendCount":"104"}}
     
    这个json字符串可以分为三个类的组合,最外层我命名为results,中间的命名为relationsInfo,最内的命名为Relations,只要明白以上关系就好办了
    以下为类定义
     
    ----------自定义类---------
    [DataContract(Name = "results")]
        public class results
        {
            [DataMember(Name = "id")]
            public string id{get ;set ; }
     
            [DataMember(Name = "result")]
            public relationsInfo result{get ;  set ;}
        }
     
          [DataContract]
        public class relationsInfo
        {
            [DataMember(Name = "relations")]
            public Relations[] relations{get; set;}
     
            [DataMember(Name = "friendCount")]
            public string friendCount{get;set;}
        }
     
         [DataContract]
        public class Relations
        {
            private string _gender;
           
            [DataMember(Name = "ptId")]
            public string ptId{ get;set;}
     
            [DataMember(Name = "game")]
            public string game{get;set;}
     
            [DataMember(Name = "gameid")]
            public string gameid {get; set;}
     
            [DataMember(Name = "worldid")]
            public string worldid{ get;set;}
     
            [DataMember(Name = "nickname")]
            public string nickname{  get;set;}
     
            [DataMember(Name = "gender")]
            public string gender
            {
                get {
                    if (_gender == "0")
                    {
                        return "男";
                    }
                    else
                    {
                        return "女";
                    }
                }
                set {
                    this._gender = value;
                }
            }
     
            [DataMember(Name = "relation")]
            public string relation{get;set ;}
        }
    ---------------转换方法-----------
     string sRelativeUsersJson; //json来源;
     JsonQueryStringConverter convert = new JsonQueryStringConverter();
     results relations = new results();
     relations = (results)convert.ConvertStringToValue(sRelativeUsersJson, relations.GetType());
    //或
    DataContractJsonSerializer jsr = new DataContractJsonSerializer(typeof(relationsInfo));
    System.IO.MemoryStream ms = new System.IO.MemoryStream(System.Text.Encoding.UTF8.GetBytes(sRelativeUsersJson));
    relationsInfo relations = (relationsInfo)jsr.ReadObject(ms);
     
  • 相关阅读:
    Linux上安装rz和sz命令
    性能测试场景设计之容量测试场景设计
    查看oracle/mysql数据库版本号
    查看weblgic/Tuxedo/WebSpere(WAS)/Tomcat中间件版本
    AIX/Linux/HP-UX查看CPU/内存/磁盘/存储命令
    常用的监控系统资源的工具
    性能瓶颈的识别方法
    性能测试的类型(负载/压力/并发/可靠性)
    Loadrunner做性能测试的主要步骤
    性能测试的主要测试阶段
  • 原文地址:https://www.cnblogs.com/MaxIE/p/2819820.html
Copyright © 2020-2023  润新知