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