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

public Class1()
        {
            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\"                     }                 ]             }         ]     } ]";
            List<KeyValue> jsonList = new List<KeyValue>();
            jsonList = DG(jsonData, jsonList);
            if (jsonList.Count > 0)
            {

            }
        }
        public class KeyValue
        {
            public string key { get; set; }
            public string value { get; set; }
        }

        public static List<KeyValue> DG(string json, List<KeyValue> strList)
        {
            //json = json.Replace("\r\n", string.Empty);
            //json = json.Replace("[", string.Empty);
            //json = json.Replace("]", string.Empty);
            JArray jar = (JArray)JsonConvert.DeserializeObject(json);
            foreach (JObject o in jar)
            {

                //var o = JObject.Parse(json);
                foreach (var x in o)
                {
                    if (x.Value.GetType() == typeof(JObject) || (x.Value.GetType() == typeof(JArray)))
                    {
                        DG(x.Value.ToString(), strList);
                    }
                    else
                    {
                        KeyValue keyValue = new KeyValue();
                        keyValue.key = x.Key;
                        keyValue.value = x.Value.ToString();
                        strList.Add(keyValue);
                    }

                }
            }
            return strList;
        }

 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值