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

[
    {
        "orderNo": "3213123123123",
        "time": "2016-09-09 12:23:33",
        "orderStatus": "1",
        "freeShipping": true,
        "fullCut": 20,
        "originalCost": 340,
        "actualPayment": 320,
        "goods": [
            {
                "UserId": "5",
                "GoodsId": "8",
                "Total": 40,
                "Number": 2,
                "ConCcoin": 0,
                "PayMode": "支付宝",
                "Price": "20.00",
                "goodsImg": "UpLoadImg/GoodsImage/546fda6d-8417-4b8f-bac6-3084dca420a9.jpg",
                "shopname": "两颗牙",
                "goodsTitle": "周村烧饼",
                "manmoney": "200",
                "jianmoney": "20",
                "jianyoufei": "8"
            },
            {
                "UserId": "5",
                "GoodsId": "7",
                "Total": 60,
                "Number": 1,
                "ConCcoin": 0,
                "PayMode": "支付宝",
                "Price": "60.00",
                "goodsImg": "UpLoadImg/GoodsImage/931be419-e9d3-4dae-ae93-5af619c217d9.jpg",
                "shopname": "两颗牙",
                "goodsTitle": "山东特产山东大枣1000g",
                "manmoney": "200",
                "jianmoney": "0",
                "jianyoufei": "10"
            }
        ]
    }
]

上面为要解析的JSON数据

 var json = "[{\"orderNo\": \"3213123123123\",\"time\": \"2016-09-09 12:23:33\",\"orderStatus\":\"1\", \"freeShipping\": true, \"fullCut\": 20,\"originalCost\": 340, \"actualPayment\": 320,\"goods\": [";
            json += " {\"UserId\": \"5\",\"GoodsId\": \"8\", \"Total\": 40, \"Number\": 2,  \"Price\": \"20.00\",  \"shopname\": \"两颗牙\", \"manmoney\": \"200\", \"jianmoney\": \"0\",\"jianyoufei\": \"10\"},";
            json += " {\"UserId\": \"5\",\"GoodsId\": \"7\", \"Total\": 60, \"Number\": 1,  \"Price\": \"60.00\",\"shopname\": \"两颗牙\", \"manmoney\": \"200\", \"jianmoney\": \"0\",\"jianyoufei\": \"10\"},";

            json += " ]}  ]";

            OrderDetails[] datas = JsonConvert.DeserializeObject<OrderDetails[]>(json);
            List<OrderDetailsInsert> insert = new List<OrderDetailsInsert>();
            foreach (OrderDetails data in datas)
            {
                var shopname = string.Empty;//判断是否同一个商家
                foreach (var item in data.goods)
                {
                    OrderDetailsInsert getinfo = new OrderDetailsInsert();
                    getinfo.orderno = data.orderno;
                    getinfo.time = data.time;
                    getinfo.orderStatus = data.orderStatus;
                    getinfo.actualPayment = data.actualPayment;
                    getinfo.orderno = data.orderno;
                    if (data.freeShipping == true)
                    {
                        getinfo.Remark = "此商品符合包邮条件及满" + item.manmoney + "" + data.fullCut + "条件:订单总金额:" + data.originalCost + "符合满减条件减去:" + data.fullCut + "实际付款金额:" + data.actualPayment;
                    }
                    else if (!string.IsNullOrEmpty(data.fullCut.ToString()) && data.fullCut != 0)
                    {
                        getinfo.Remark = "此商品符合满" + item.manmoney + "" + data.fullCut + "条件:订单总金额:" + data.originalCost + "符合满减条件减去:" + data.fullCut + "实际付款金额:" + data.actualPayment;
                    }
                    else
                    {
                        getinfo.Remark = "订单实际付款金额:" + data.actualPayment;
                    }
                    getinfo.GoodsId = item.GoodsId;
                    getinfo.Total = item.Total;
                    getinfo.Number = item.Number;
                    getinfo.Price = item.Price;
                    insert.Add(getinfo);
                }
            }

要用的对象类

 public class OrderDetailsInsert
    {
        public string orderno { get; set; }
        public DateTime time { get; set; }
        public char orderStatus { get; set; }
   
        public Decimal actualPayment { get; set; }

        public int GoodsId { get; set; }
        public string Total { get; set; }
        public int Number { get; set; }
        public string Price { get; set; }
        public string Remark { get; set; }
    }  


    public class OrderDetails
    {
        public string orderno { get; set; }
        public DateTime time { get; set; }
        public char orderStatus { get; set; }
        public bool freeShipping { get; set; }
        public Decimal fullCut { get; set; }
        public Decimal originalCost { get; set; }
        public Decimal actualPayment { get; set; }

        public GoodsInfoList[] goods { get; set; }

    }  

    public class GoodsInfoList
    {
        public int UserId { get; set; }
        public int GoodsId { get; set; }
        public string Total { get; set; }
        public int Number { get; set; }
        public string Price { get; set; }
        public string shopname { get; set; }
        public string manmoney { get; set; }
 
    }

效果图:

参考资料:

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

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

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

猜你喜欢

转载自www.cnblogs.com/bwlluck/p/9233299.html