码迷,mamicode.com
首页 > Windows程序 > 详细

c# jobject 的数据结构的解析:

时间:2019-02-19 10:26:17      阅读:651      评论:0      收藏:0      [点我收藏+]

标签:sof   官网   假设   代码   数据信息   name   jar   token   nbsp   

c# jobject 数据结构的解析:
首先下载Newtonsoft.Json,增加引用using Newtonsoft.Json.Linq;
把jobject的内容提取出来,Jobject的内容格式如下:

{
 "code": 200,
 "msg": "SUCCESS",
 "data": {
  "id": "12345678",
  "name": "张三",
  "sex": "男",
  "result": {
   "access_token": "49d58eacd7811e463429a1ae10b42173",
   "user_info": [{
    "school": "社会大学",
    "major": "软件开发",
    "education": "本科",
    "score": 97
   }, {
    "school": "湖南大学",
    "major": "软件工程",
    "education": "研究生",
    "score": 100
   }]
  }
 }
}

可放到json官网在线JSON校验格式化工具里解析。

 

代码如下:                   

1,新建类:
        public class UserInfo
        {
            public string id { get; set; }
            public string name { get; set; }
            public string sex { get; set; }
            public string access_token { get; set; }
            public string school { get; set; }
            public string major { get; set; }
            public string education { get; set; }
            public string score { get; set; }
        }

2,获取值:

            JObject result = new JObject();//假设result为数据结构
            UserInfo userinfo = new UserInfo();
            userinfo.id = result["data"].Value<string>("id");//id
            userinfo.name = result["data"].Value<string>("name"); //name
            userinfo.sex = result["data"].Value<string>("sex"); //sex
            userinfo.access_token= result["data"]["result"]["access_token"].ToString();//access_token
            JArray res = result["data"]["result"].Value<JArray>("user_info");
            JObject obj = JObject.Parse(res[0].ToString());//只获取数据结构中第一个userinfo里的数据信息
            userinfo.school = obj.Value<string>("school"); //schoool
            userinfo.major = obj.Value<string>("major");//major
            userinfo.education = obj.Value<string>("education");//education
            userinfo.score= obj.Value<string>("score");//score

c# jobject 的数据结构的解析:

标签:sof   官网   假设   代码   数据信息   name   jar   token   nbsp   

原文地址:https://www.cnblogs.com/cqj98k/p/10399394.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!