C# Newtonsoft.Json 解析多嵌套json 进行反序列化
Posted 阳光总在风雨后
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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
以上是关于C# Newtonsoft.Json 解析多嵌套json 进行反序列化的主要内容,如果未能解决你的问题,请参考以下文章
C# Newtonsoft.Json.dll json数据格式上传下载解析
C# Newtonsoft.Json解析json字符串处理 - JToken 用法
如何使用 Newtonsoft.Json 将包含数组数组的 json 对象解析为 C# 中的对象列表?
C# Newtonsoft.Json解析json字符串处理(最清晰易懂的方法)