这里用到了一个比较方便的程序包Newtonsoft
程序管理器控制台中输入命令:Install-Package Newtonsoft.Json即可
引用:
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
static void testHttp()
{
var postUrl = "http://yoururl";
Dictionary<string, string> postData = new Dictionary<string, string>();
postData.Add("userID", "100000048");
postData.Add("gradeID", "1");
// 自己实现的http请求
string postStr = HttpRequestHelper.DictoryToGetParam(postData);
var repose = HttpRequestHelper.HttpPostRequest(postUrl, postStr);
Console.WriteLine(repose + "\r\n");
object product = JsonConvert.DeserializeObject(repose);
Console.WriteLine(product);
var jo = JObject.Parse(repose);
foreach (var m in jo)
{
Console.WriteLine("key : " + m.Key);
Console.WriteLine(m);
Console.WriteLine(m.Value);
}
Console.WriteLine(jo.Value<object>("returnResult"));
JObject jo2 = (JObject)JsonConvert.DeserializeObject(repose);
Console.WriteLine(Convert.ToString(jo2["returnResult"]["abc"]));//Convert.ToString(null)不会抛出异常,null.ToString()会抛出异常
Console.WriteLine(Convert.ToString(jo2["returnResult"]["activity_name"]));
Console.ReadLine();
}
结果:
{"returnResult":{"school_name":"\u8d85\u7ea7\u8f85\u5bfc\u6700\u5f3a\u5bb6\u957f\u7fa4",
"grades":[{"grade_id":"1","school_id":"1","grade_des":"\u521d\u4e2d","grade_doc":"",
"create_time":null,"grade_qrcode_img":null},
{"grade_id":"2","school_id":"1","grade_des":"\u9ad8\u4e2d","grade_doc":"",
"create_time":null,"grade_qrcode_img":null}]},"statusCode":0,"errorMsg":""}
{
"returnResult": {
"school_name": "希望小学",
"grades": [
{
"grade_id": "1",
"school_id": "1",
"grade_des": "初中",
"grade_doc": "",
"create_time": null,
"grade_qrcode_img": null
},
{
"grade_id": "2",
"school_id": "1",
"grade_des": "高中",
"grade_doc": "",
"create_time": null,
"grade_qrcode_img": null
}
]
},
"statusCode": 0,
"errorMsg": ""
}
key : returnResult
[returnResult, {
"school_name": "希望小学",
"grades": [
{
"grade_id": "1",
"school_id": "1",
"grade_des": "初中",
"grade_doc": "",
"create_time": null,
"grade_qrcode_img": null
},
{
"grade_id": "2",
"school_id": "1",
"grade_des": "高中",
"grade_doc": "",
"create_time": null,
"grade_qrcode_img": null
}
]
}]
{
"school_name": "希望小学",
"grades": [
{
"grade_id": "1",
"school_id": "1",
"grade_des": "初中",
"grade_doc": "",
"create_time": null,
"grade_qrcode_img": null
},
{
"grade_id": "2",
"school_id": "1",
"grade_des": "高中",
"grade_doc": "",
"create_time": null,
"grade_qrcode_img": null
}
]
}
key : statusCode
[statusCode, 0]
0
key : errorMsg
[errorMsg, ]
{
"school_name": "希望小学",
"grades": [
{
"grade_id": "1",
"school_id": "1",
"grade_des": "初中",
"grade_doc": "",
"create_time": null,
"grade_qrcode_img": null
},
{
"grade_id": "2",
"school_id": "1",
"grade_des": "高中",
"grade_doc": "",
"create_time": null,
"grade_qrcode_img": null
}
]
}
希望小学
结论
JsonConvert.DeserializeObject(repose);解析json字符串,php后台返回的json格式的字符串
var jo = JObject.Parse(repose);返回JObject类型数据,JObject可以通过["property_name"]获取property_name对应的数据
这是数据又是一个JObject类型数据,如此循环往复
foreach (var m in jo)
{
Console.WriteLine("key : " + m.Key);
Console.WriteLine(m);
Console.WriteLine(m.Value);
}
Console.WriteLine(jo.Value<object>("returnResult"));
好用的方法
JObject jo2 = (JObject)JsonConvert.DeserializeObject(repose);
Console.WriteLine(Convert.ToString(jo2["returnResult"]["abc"]));//Convert.ToString(null)不会抛出异常,null.ToString()会抛出异常
Console.WriteLine(Convert.ToString(jo2["returnResult"]["activity_name"]));
循环往复的使用["property_name"]["property_name"]["property_name"]获取属性值,如果是数组,可以用[0]取值,继续["property_name"][0]["property_name"]取值