处理较复杂的Json字符串,不需要对Json进行序列化操作,以下是一种非常简单的方法,记录以下;
Json字符串如下:
{ "code": 0, "msg": "SUCCESS", "result": [{ "changesContent": [{ "unionNo": "1719708390162472960", "orderDeliveredList": [{ "expName": "普通快递", "expNo": "333", "packageList": [{ "deliveredNum": 1, "deliveredTime": "2023-11-01 21:31:04", "orderDetailId": 3566376 }], "suborderId": 3366323, "expCode": "XUNI" }] }, { "unionNo": "1719706286249254912", "orderDeliveredList": [{ "expName": "速必达物流", "expNo": "111", "packageList": [{ "deliveredNum": 2, "deliveredTime": "2023-11-01 21:31:10", "orderDetailId": 3566375 }], "suborderId": 3366322, "expCode": "SUBIDA" }] }], "eventDesc": "订单发货通知", "eventKey": "ORDER_DELIVERY" }], "traceId": "7c2aca0b-5108236" }
使用JObject类来处理,代码如下:
string orderNum = string.Empty; string jsonStr = @"{ ""code"":0, ""msg"":""SUCCESS"", ""result"":[ { ""changesContent"":[ { ""unionNo"":""1719708390162472960"", ""orderDeliveredList"":[ { ""expName"":""普通快递"", ""expNo"":""333"", ""packageList"":[ { ""deliveredNum"":1, ""deliveredTime"":""2023-11-01 21:31:04"", ""orderDetailId"":3566376 } ], ""suborderId"":3366323, ""expCode"":""XUNI"" } ] }, { ""unionNo"":""1719706286249254912"", ""orderDeliveredList"":[ { ""expName"":""速必达物流"", ""expNo"":""111"", ""packageList"":[ { ""deliveredNum"":2, ""deliveredTime"":""2023-11-01 21:31:10"", ""orderDetailId"":3566375 } ], ""suborderId"":3366322, ""expCode"":""SUBIDA"" } ] } ], ""eventDesc"":""订单发货通知"", ""eventKey"":""ORDER_DELIVERY"" } ], ""traceId"":""7c2aca0b-5108236"" }"; JObject jsonObjectStr = JObject.Parse(jsonStr); JToken[] resultArr = jsonObjectStr.GetValue("result").ToArray(); if(resultArr.Length>0) { var result = jsonObjectStr.SelectTokens("result[*].changesContent[*].unionNo").Select(a => a.Value<string>()).ToList(); foreach(string item in result) { orderNum = item; } }
标签:01,deliveredNum,21,处理,orderDetailId,json,unionNo,result,net From: https://www.cnblogs.com/webyu/p/17804912.html