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
}
]
}
}
}
代码如下:
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
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对的支持。如果你想了解更多相关内容请查看下面相关链接
相关文章