• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

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

c# 搞代码 4年前 (2022-01-09) 22次浏览 已收录 0个评论

我就废话不多说啦,大家还是直接看代码吧~

[
 {
  "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\", \"T<div>本文来源gaodai.ma#com搞#代!码网_</div>otal\": 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; }
 
 }

搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:C# Newtonsoft.Json 解析多嵌套json 进行反序列化的实例
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址