获取Json中特定的值
假如我們得到了一個(gè)json的數(shù)據(jù):json===》? ?{"Head":{"TransCode":"X1009","TransId":"20190514145005iQXk"},"Body":{"OrderSN":"95131406006","WHSE":"aaaaa","SNs":[{"SN":"HCMPFD000621"},{"SN":"1212122"}]}}
類似這樣。我們要取出來(lái)里面特定的值,而且,SNs還是一個(gè)集合。
方法如下:
string OrderSn = json["Body"]["OrderSN"].ToString();
string WHSE = json["Body"]["WHSE"].ToString();
string SNs = json["Body"]["SNs"].ToString();
SNs是一個(gè)集合,怎么取出來(lái)呢:
將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() + ",";}}?
這樣子就可以循環(huán)出來(lái)里面的值了。
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 string shop_name = j["Success"]["items"]["item"][0]["shop_name"].ToString();//獨(dú)一味日化旗艦店、、、一層層的層級(jí)關(guān)系?
{"Success": {"total_results": "1","items": {"item": [{ "shop_name": "獨(dú)一味日化旗艦店", "serial_num": "","order_channel": "直營(yíng)網(wǎng)店", "updatetime": "","總數(shù)量": "3739","maxrowver": "","tid_item": [{"storage_id": "1","tid": "S17fdgfgfg0349","pro_detail_code": "191", "book_inventory": "-5.000", "store_location": "","pro_type": "原始產(chǎn)品"}, {"storage_id": "1","tid": "S1712220000349","pro_detail_code": "152", "book_inventory": "-4.000", "store_location": "","pro_type": "原始產(chǎn)品"}]}]}} }上面這個(gè)是其他的廠商返回的一個(gè)Json格式的數(shù)據(jù),現(xiàn)在可以是Json嵌套的還挺多,其中的一個(gè)項(xiàng)次還有數(shù)組,現(xiàn)在就是把這些數(shù)據(jù)撈出來(lái)存下來(lái)。
var j = JObject.Parse(response);//這個(gè)就是主干的值string shop_name = j["Success"]["items"]["item"][0]["shop_name"].ToString();//獨(dú)一味日化旗艦店string tid_item = j["Success"]["items"]["item"][0]["tid_item"].ToString();
//下面是獲取數(shù)組的名細(xì)可以取出來(lái)值JArray jArray = JArray.Parse(tid_item);for (int i = 0; i < jArray.Count; i++){JObject jdata = (JObject)jArray[i];string pro_detail_code = jdata["pro_detail_code"].ToString();}
//將數(shù)據(jù)動(dòng)態(tài)插入到數(shù)組中去
JArray jArray = JArray.Parse(tid_item);JArray arr = new JArray(); for (int i = 0; i < jArray.Count; i++){JObject obj = new JObject();JObject jdata = (JObject)jArray[i];obj["linenum"] = jdata["cost_price"].ToString();obj["sku"] = jdata["sell_price"].ToString();obj["qty"] = jdata["original_price"].ToString();obj["inventoryname"] = jdata["sys_price"].ToString();obj["price"] = jdata["product_name"].ToString();arr.Add(obj);}var rss = new JObject{{"Head",new JObject{{"TransCode","X1009" },{"TransId",1212}}},{ "Body", new JObject{{"orderno", "orderno" },{"billto","billto"},{"shipto", "shipto"},{"workid", "workid"},{"tocity", "tocity"},{"Address", "Address"},{"Contact", "Contact"},{"Tel", "Tel"},{"detail", arr }}}};
?
轉(zhuǎn)載于:https://www.cnblogs.com/ZkbFighting/p/10909892.html
總結(jié)
以上是生活随笔為你收集整理的获取Json中特定的值的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: java常用类--------File类
- 下一篇: 数字图像处理及MATLAB实现实验五——