C# Newtonsoft.Json 解析多嵌套json 进行反序列化的实例
作者:蝶恋花雨 时间:2022-04-09 11:23:13
我就废话不多说啦,大家还是直接看代码吧~
[
{
"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;
}
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。如有错误或未考虑完全的地方,望不吝赐教。
来源:https://blog.csdn.net/kongwei521/article/details/52488977
标签:C#,多嵌套,json,反序列化
0
投稿
猜你喜欢
详解Struts2动态方法调用
2022-10-18 11:19:25
C#中怎样从指定字符串中查找并替换字符串?
2023-09-30 14:26:24
详谈Java中的Object、T(泛型)、?区别
2022-06-11 21:13:11
Javaweb基础入门HTML之table与form
2022-11-28 16:09:16
JVM内存参数配置详解
2022-05-20 19:17:57
Java线程生命周期的终止与复位
2022-04-22 01:57:01
Java 实现将List平均分成若干个集合
2023-10-16 21:36:59
spring缓存自定义resolver的方法
2021-05-30 17:07:56
基于FLink实现实时安全检测的示例代码
2022-05-06 11:03:11
Mybatis中Collection集合标签的使用详解
2023-07-10 05:09:35
Android程序锁的实现以及逻辑
2022-09-29 19:50:57
JDK常用命令jps jinfo jstat的具体说明与示例
2021-08-09 16:03:30
Java将json对象转换为map键值对案例详解
2023-11-28 07:38:43
详解mybatis-plus配置找不到Mapper接口路径的坑
2022-03-12 10:37:45
Android 用HttpURLConnection访问网络的方法
2023-05-18 17:33:01
Android SeekBar实现平滑滚动
2022-01-21 10:51:24
详解Android Service与Activity之间通信的几种方式
2023-10-26 04:15:48
浅谈Mybatis之参数传递的几种姿势
2021-06-20 01:26:54
详解C#实现MD5加密的示例代码
2023-11-28 21:01:54
SpringBoot+Vue.js实现前后端分离的文件上传功能
2023-08-10 08:11:18