• C# Newtonsoft.Json 解析多嵌套json 进行反序列化


    [javascript] view plain copy
     
    1. [  
    2.     {  
    3.         "orderNo": "3213123123123",  
    4.         "time": "2016-09-09 12:23:33",  
    5.         "orderStatus": "1",  
    6.         "freeShipping": true,  
    7.         "fullCut": 20,  
    8.         "originalCost": 340,  
    9.         "actualPayment": 320,  
    10.         "goods": [  
    11.             {  
    12.                 "UserId": "5",  
    13.                 "GoodsId": "8",  
    14.                 "Total": 40,  
    15.                 "Number": 2,  
    16.                 "ConCcoin": 0,  
    17.                 "PayMode": "支付宝",  
    18.                 "Price": "20.00",  
    19.                 "goodsImg": "UpLoadImg/GoodsImage/546fda6d-8417-4b8f-bac6-3084dca420a9.jpg",  
    20.                 "shopname": "两颗牙",  
    21.                 "goodsTitle": "周村烧饼",  
    22.                 "manmoney": "200",  
    23.                 "jianmoney": "20",  
    24.                 "jianyoufei": "8"  
    25.             },  
    26.             {  
    27.                 "UserId": "5",  
    28.                 "GoodsId": "7",  
    29.                 "Total": 60,  
    30.                 "Number": 1,  
    31.                 "ConCcoin": 0,  
    32.                 "PayMode": "支付宝",  
    33.                 "Price": "60.00",  
    34.                 "goodsImg": "UpLoadImg/GoodsImage/931be419-e9d3-4dae-ae93-5af619c217d9.jpg",  
    35.                 "shopname": "两颗牙",  
    36.                 "goodsTitle": "山东特产山东大枣1000g",  
    37.                 "manmoney": "200",  
    38.                 "jianmoney": "0",  
    39.                 "jianyoufei": "10"  
    40.             }  
    41.         ]  
    42.     }  
    43. ]  

    上面为要解析的JSON数据

    [csharp] view plain copy
     
    1. var json = "[{"orderNo": "3213123123123","time": "2016-09-09 12:23:33","orderStatus":"1", "freeShipping": true, "fullCut": 20,"originalCost": 340, "actualPayment": 320,"goods": [";  
    2.            json += " {"UserId": "5","GoodsId": "8", "Total": 40, "Number": 2,  "Price": "20.00",  "shopname": "两颗牙", "manmoney": "200", "jianmoney": "0","jianyoufei": "10"},";  
    3.            json += " {"UserId": "5","GoodsId": "7", "Total": 60, "Number": 1,  "Price": "60.00","shopname": "两颗牙", "manmoney": "200", "jianmoney": "0","jianyoufei": "10"},";  
    4.   
    5.            json += " ]}  ]";  
    6.   
    7.            OrderDetails[] datas = JsonConvert.DeserializeObject<OrderDetails[]>(json);  
    8.            List<OrderDetailsInsert> insert = new List<OrderDetailsInsert>();  
    9.            foreach (OrderDetails data in datas)  
    10.            {  
    11.                var shopname = string.Empty;//判断是否同一个商家  
    12.                foreach (var item in data.goods)  
    13.                {  
    14.                    OrderDetailsInsert getinfo = new OrderDetailsInsert();  
    15.                    getinfo.orderno = data.orderno;  
    16.                    getinfo.time = data.time;  
    17.                    getinfo.orderStatus = data.orderStatus;  
    18.                    getinfo.actualPayment = data.actualPayment;  
    19.                    getinfo.orderno = data.orderno;  
    20.                    if (data.freeShipping == true)  
    21.                    {  
    22.                        getinfo.Remark = "此商品符合包邮条件及满" + item.manmoney + "减" + data.fullCut + "条件:订单总金额:" + data.originalCost + "符合满减条件减去:" + data.fullCut + "实际付款金额:" + data.actualPayment;  
    23.                    }  
    24.                    else if (!string.IsNullOrEmpty(data.fullCut.ToString()) && data.fullCut != 0)  
    25.                    {  
    26.                        getinfo.Remark = "此商品符合满" + item.manmoney + "减" + data.fullCut + "条件:订单总金额:" + data.originalCost + "符合满减条件减去:" + data.fullCut + "实际付款金额:" + data.actualPayment;  
    27.                    }  
    28.                    else  
    29.                    {  
    30.                        getinfo.Remark = "订单实际付款金额:" + data.actualPayment;  
    31.                    }  
    32.                    getinfo.GoodsId = item.GoodsId;  
    33.                    getinfo.Total = item.Total;  
    34.                    getinfo.Number = item.Number;  
    35.                    getinfo.Price = item.Price;  
    36.                    insert.Add(getinfo);  
    37.                }  
    38.            }  

    要用的对象类
    [csharp] view plain copy
     
    1. public class OrderDetailsInsert  
    2.    {  
    3.        public string orderno { get; set; }  
    4.        public DateTime time { get; set; }  
    5.        public char orderStatus { get; set; }  
    6.     
    7.        public Decimal actualPayment { get; set; }  
    8.   
    9.        public int GoodsId { get; set; }  
    10.        public string Total { get; set; }  
    11.        public int Number { get; set; }  
    12.        public string Price { get; set; }  
    13.        public string Remark { get; set; }  
    14.    }    
    15.   
    16.   
    17.    public class OrderDetails  
    18.    {  
    19.        public string orderno { get; set; }  
    20.        public DateTime time { get; set; }  
    21.        public char orderStatus { get; set; }  
    22.        public bool freeShipping { get; set; }  
    23.        public Decimal fullCut { get; set; }  
    24.        public Decimal originalCost { get; set; }  
    25.        public Decimal actualPayment { get; set; }  
    26.   
    27.        public GoodsInfoList[] goods { get; set; }  
    28.   
    29.    }    
    30.   
    31.    public class GoodsInfoList  
    32.    {  
    33.        public int UserId { get; set; }  
    34.        public int GoodsId { get; set; }  
    35.        public string Total { get; set; }  
    36.        public int Number { get; set; }  
    37.        public string Price { get; set; }  
    38.        public string shopname { get; set; }  
    39.        public string manmoney { get; set; }  
    40.   
    41.    }  

    效果图:

    参考资料:

    http://blog.csdn.net/chinacsharper/article/details/9246627

    http://blog.csdn.net/sgear/article/details/7587705

    http://blog.csdn.net/leftfist/article/details/42435887

     
    0
  • 相关阅读:
    java课堂作业--异常处理
    Node.js 应用---定时给自己发送邮件
    JAVA课堂作业(2019.10.21)
    添加学生信息系统
    Hdfs的java必会Api操作
    架构之美2
    mybatis知识点03
    mybatis知识点总结02
    mybatis知识点总结01
    第四周周总结
  • 原文地址:https://www.cnblogs.com/zxtceq/p/7520846.html
Copyright © 2020-2023  润新知