json 递归

using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace ConsoleApp11
{
    class Program
    {
        static void Main(string[] args)
        {
            #region
            string jsonText = "[{\"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\"                     }                 ]             }         ]     } ]";
            //Dictionary<string, object> dd = jsonText.Trim(new char[] { ‘{‘, ‘}‘ }).Split(‘,‘).ToDictionary(s => s.Split(‘:‘)[0], s => (object)s.Split(‘:‘)[1]);
            //JObject json1 = (JObject)JsonConvert.DeserializeObject(jsonText);
            //JArray array = (JArray)json1["W_OFF_BURSUBSIDY"];

            //int i = array.Count;

            //string aa = "";
            //foreach (var jObject in array)
            //{
            //    //赋值属性
            //    aa = jObject["USER_NAME"].ToString();//获取字符串中id值
            //}
            //Console.WriteLine(aa);
            #endregion
            List<KeyValue> jsonList = new List<KeyValue>();
            jsonList = DG(jsonText, jsonList);
            if (jsonList.Count > 0)
            {

            }
        }
        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;
        }
    }
    public class KeyValue
    {
        public string key { get; set; }
        public string value { get; set; }
    }

    
}

 

json 递归

上一篇:Centos7找不到ifconfig和netstat命令


下一篇:dotnet Framework的aop和dotnet net core的aop