当前位置:   article > 正文

C# 使用递归读取 JSON 数据(包含层级数据)_c# json 上下级

c# json 上下级
  1. public Class1()
  2. {
  3. string jsonData = "[{\"code\": \"LH201909100021\", \"customerCode\": \"80000000\", \"depositAmount\": \"179\", \"memberCode\": \"\", \"netAmount\": \"179\", \"orderAmount\": \"179\", \"orderType\": \"Z123\", \"pointOfService\": \"K900QTD014\", \"promotionDiscount\": \"0\", \"relPosOrder\": \"\", \"saleDate\": \"20191011172645\", \"salesCardType\": \"\", \"shopassistant\": \"onlinery\", \"specialDiscount\": \"\", \"standardDiscount\": \"\", \"temporaryDiscount\": \"\", \"totalDiscount\": \"0\" }, { \"code\": \"K210QTD0021191009092742\", \"customerCode\": \"0020005000\", \"depositAmount\": \"0\", \"memberCode\": \"SQ\", \"netAmount\": \"60\", \"orderAmount\": \"60\", \"orderType\": \"Z112\", \"pointOfService\": \"K210QTD002\", \"promotionDiscount\": \"0\", \"relPosOrder\": \"\", \"saleDate\": \"20191009093011\", \"salesCardType\": \"SQ\", \"shopassistant\": \"999\", \"specialDiscount\": \"0\", \"standardDiscount\": \"0\", \"temporaryDiscount\": \"0\", \"totalDiscount\": \"0\", \"hpaymentInfos\": [ { \"cardCode\": \"\", \"cardDisType\": \"\", \"discountAmount\": \"\", \"payAmount\": \"60\", \"paymentType\": \"ZG01\", \"pointAmount\": \"\", \"thirdDiscount\": \"\", \"thirdSubsidy\": \"\" } ], \"posOrderEntries\": [ { \"actualAmount\": \"30\", \"discountAmount\": \"0\", \"entryNumber\": \"1\", \"extracolumn\": \"K210QTD0021191009092742\", \"minsqty\": \"0\", \"netAmount\": \"60\", \"productCode\": \"000000008040600003\", \"promotionDiscount\": \"\", \"quantity\": \"2\", \"specialDiscount\": \"0\", \"standardDiscount\": \"0\", \"systemAmount\": \"30\", \"temporaryDiscount\": \"0\", \"cardIds\": [ { \"cardIds\": \"400000004452136649-400000004452136649\", \"quantity\": \"1\" }, { \"cardIds\": \"400000004452157237-400000004452157237\", \"quantity\": \"1\" } ] } ] } ]";
  4. List<KeyValue> jsonList = new List<KeyValue>();
  5. jsonList = DG(jsonData, jsonList);
  6. if (jsonList.Count > 0)
  7. {
  8. }
  9. }
  10. public class KeyValue
  11. {
  12. public string key { get; set; }
  13. public string value { get; set; }
  14. }
  15. public static List<KeyValue> DG(string json, List<KeyValue> strList)
  16. {
  17. //json = json.Replace("\r\n", string.Empty);
  18. //json = json.Replace("[", string.Empty);
  19. //json = json.Replace("]", string.Empty);
  20. JArray jar = (JArray)JsonConvert.DeserializeObject(json);
  21. foreach (JObject o in jar)
  22. {
  23. //var o = JObject.Parse(json);
  24. foreach (var x in o)
  25. {
  26. if (x.Value.GetType() == typeof(JObject) || (x.Value.GetType() == typeof(JArray)))
  27. {
  28. DG(x.Value.ToString(), strList);
  29. }
  30. else
  31. {
  32. KeyValue keyValue = new KeyValue();
  33. keyValue.key = x.Key;
  34. keyValue.value = x.Value.ToString();
  35. strList.Add(keyValue);
  36. }
  37. }
  38. }
  39. return strList;
  40. }

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/你好赵伟/article/detail/977746
推荐阅读
相关标签
  

闽ICP备14008679号