怎么用C#获取这个json中的某个值
{{"error_code":0,"error_msg":"SUCCESS","log_id":3639764640,"timestamp":1525952378,"ca...
{{
"error_code": 0,
"error_msg": "SUCCESS",
"log_id": 3639764640,
"timestamp": 1525952378,
"cached": 0,
"result": {
"face_token": "f2aa605b2436c48ec23e6ca9810eabc1",
"user_list": [
{
"group_id": "uid_01",
"user_id": "uid_01_02",
"user_info": "",
"score": 96.937515258789
}
]
}
}}
想取score的值 展开
"error_code": 0,
"error_msg": "SUCCESS",
"log_id": 3639764640,
"timestamp": 1525952378,
"cached": 0,
"result": {
"face_token": "f2aa605b2436c48ec23e6ca9810eabc1",
"user_list": [
{
"group_id": "uid_01",
"user_id": "uid_01_02",
"user_info": "",
"score": 96.937515258789
}
]
}
}}
想取score的值 展开
1个回答
2018-05-10
展开全部
上面是完整代码,在图片上,鼠标右键,选择在新窗口打开,是大图。这里其实是C#的序列化和反序列化。下面是分析。
看到JS的{}就用c#对象,看到JS的[]就用c#数组。从里往外:user_list里面是用户信息,
定义一个类,类随便叫,但是变量必须和JSON一样,例如叫做Person
public class Users {
public string group_id;
public string user_id;
public string user_info;
public string score;
}
接下来是result这层。
public class Result
{
public string face_token;
public Users[] user_list;
}
再来最外面的这层
public class Json {
public string error_code;
public string error_msg;
public string log_id;
public string timestamp;
public string cached;
public Result result;
}
最后:
string strJson = System.IO.File.ReadAllText(Server.MapPath("json.txt"));
DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(Json));
var mStream = new MemoryStream(Encoding.Default.GetBytes(strJson));
Json json = (Json)serializer.ReadObject(mStream);
Result r = json.result;
Users[] ulist = r.user_list;
Users u = ulist[0];
Response.Write(u.score);
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询