假如我们得到了一个json的数据:json===》 {"Head":{"TransCode":"X1009","TransId":"20190514145005iQXk"},"Body":{"OrderSN":"95131406006","WHSE":"aaaaa","SNs":[{"SN":"HCMPFD000621"},{"SN":"1212122"}]}}
类似这样。我们要取出来里面特定的值,而且,SNs还是一个集合。
方法如下:
string OrderSn = json["Body"]["OrderSN"].ToString();
string WHSE = json["Body"]["WHSE"].ToString();
string SNs = json["Body"]["SNs"].ToString();
SNs是一个集合,怎么取出来呢:
将json字符串解析为JToken,JObject是JToken的父类,直接使用JToken比较好用
dynamic Sns =JToken.Parse(SNs) as dynamic
for (int i = 0; i < Sns.Count; i++)//获取备注里的SN
{
if (Sns[i].SN.ToString() != "")
{
str += Sns[i].SN.ToString() + ",";
}
}
这样子就可以循环出来里面的值了。
1 HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse(); 2 string response = string.Empty; 3 using (StreamReader reader = new StreamReader(httpWebResponse.GetResponseStream())) 4 response = reader.ReadToEnd(); 5 6 var j = JObject.Parse(response); 7