阅读 85

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 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 jsonList = new List();
            jsonList = DG(jsonText, jsonList);
            if (jsonList.Count > 0)
            {

            }
        }
        public static List DG(string json, List 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; }
    }

    
}

 

原文:https://www.cnblogs.com/HuangLiming/p/14819192.html

文章分类
代码人生
文章标签
版权声明:本站是系统测试站点,无实际运营。本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 XXXXXXo@163.com 举报,一经查实,本站将立刻删除。
相关推荐