打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
c# jobject 的数据结构的解析:

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

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
.net(c#)提取多层嵌套的JSON
Web API接口设计经验总结
优雅的在WinForm/WPF/控制台 中使用特性封装WebApi
【C#】Newtonsoft.Json 中 JArray 添加数组报错:Could not determine JSON object type for type 'xxx'
微信企业号开发步骤
将json格式数据重复的数据,值相加后移除一条
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服