C# Newtonsoft.Json 解析多嵌套json 进行反序列化的实例
蝶恋花雨 人气:0这篇文章主要介绍了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; } }
效果图:
补充:c#使用 Newtonsoft.Json 实现多态序列化
有时候会碰到以下情况
1.定义了一个类型为A的字段,但在实例化时使用的是它的派生类B。
2.定义了一个存储基类型A的集合,但实际上里面包含了各种各样的派生类。
上述两种情况在序列化的时候没有问题,但在反序列化的时候程序并不能将其转化为成派生类。
Newtonsoft.Json提供了一个JsonConverter特性,我们可以自定义某个数据的json读写方式以达成多态序列化的目的。
1.自定义转化类
using System; using System.Collections.Generic; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using UnityEngine; namespace XFramework.JsonConvter { /// <summary> /// 用于多态列表的转化 /// </summary> /// <typeparam name="T"></typeparam> public class PolyListConverter<T> : JsonConverter { public override bool CanConvert(Type objectType) { return true; } public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { var jObject = JObject.Load(reader); List<T> values = new List<T>(); foreach (var item in jObject.Properties()) { Type type = Type.GetType(item.Name); var value = item.Value.ToObject(type); values.Add((T)value); } return values; } public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { var values = (List<T>)value; JObject jObject = new JObject(); foreach (var item in values) { jObject.Add(item.GetType().FullName, JToken.FromObject(item)); } var p = jObject.Properties(); foreach (var item in p) { Debug.Log(item.Name); } serializer.Serialize(writer, jObject); } } /// <summary> /// 用于多态序列化 /// </summary> public class PolyConverter : JsonConverter { public override bool CanConvert(Type objectType) { return true; } public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { var jObject = JObject.Load(reader); foreach (var item in jObject.Properties()) { Type type = Type.GetType(item.Name); var value = item.Value.ToObject(type); return value; } return null; } public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) { JObject jObject = new JObject(); jObject.Add(value.GetType().FullName, JToken.FromObject(value)); serializer.Serialize(writer, jObject); } } }
2.使用方式
DataBase是自己定义的一个数据基类,按照以下写法,再利用Newtonsoft 的API 序列化和反序列化TestData就可以成功实现多态读写了。
using XFramework.JsonConvter; using Newtonsoft.Json; public class TestData { // 对应第一种情况 [JsonConverter(typeof(PolyConverter))] public DataBase datas; // 对应第二种情况 [JsonConverter(typeof(PolyListConverter<DataBase>))] public List<EquipPowerBase> powerList; }
以上为个人经验,希望能给大家一个参考,也希望大家多多支持。如有错误或未考虑完全的地方,望不吝赐教。
加载全部内容