如何反序列化JSON字符串对象名单在C#点 [英] How to deserialize json string to object list in c# dot

查看:128
本文介绍了如何反序列化JSON字符串对象名单在C#点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用下面的JSON字符串工作

  {
交易:
[
   {
    paymentcharge:0.0,
    量:352,
    ID:13418,
    shippingcharge:35,
    shippingtype:2,
    状态:2,
    paymenttype:1,
    日期:2012年10月6日16:15:28.0
   },
   {
    paymentcharge:0.0,
    量:42455,
    ID:16305,
    shippingcharge:0,
    shippingtype:2,
    状态:2,
    paymenttype:2,
    日期:二零一二年十一月三十〇日09:29:29.0
   },
   {
    paymentcharge:1.0,
    量:42456,
    ID:16305,
    shippingcharge:0,
    shippingtype:2,
    状态:2,
    paymenttype:2,
    日期:二零一二年十一月三十〇日09:29:29.0
   }
]
伯爵:3
}
 


我有一类结构如下解析,感觉JSON数据

 类clsSalesTran
{
    公共双paymentcharge {获得;组; }
    公共双倍金额{获得;组; }
    众长ID {获得;组; }
    公众诠释shippingcharge {获得;组; }
    公众诠释shippingtype {获得;组; }
    公众诠释状态{获得;组; }
    公众诠释paymenttype {获得;组; }
    公开日期时间日期{获得;组; }
}
 


我如何反序列化上面的JSON字符串转换成列表?

  

我使用的 Newtonsoft.Json 的用于反序列化。

解决方案

首先创建另一个类:

 公共类SalesTransactions
{
     公开名单< clsSalesTran>交易{获取;集;}
     公众诠释计数{获取;集;}
}
 

然后用

  JsonConvert.DeserializeObject< SalesTransactions>(inputString)
 

I am working with the following JSON string

{
"transactions": 
[
   {
    "paymentcharge":"0.0",
    "amount":352,
    "id":13418,
    "shippingcharge":35,
    "shippingtype":2,
    "status":2,
    "paymenttype":1,
    "date":"2012-10-06 16:15:28.0"
   },   
   {
    "paymentcharge":"0.0",
    "amount":42455,
    "id":16305,
    "shippingcharge":0,
    "shippingtype":2,
    "status":2,
    "paymenttype":2,
    "date":"2012-11-30 09:29:29.0"
   },   
   {
    "paymentcharge":"1.0",
    "amount":42456,
    "id":16305,
    "shippingcharge":0,
    "shippingtype":2,
    "status":2,
    "paymenttype":2,
    "date":"2012-11-30 09:29:29.0"
   }
],
"count":3
}


I have a class structure as follows for parsing and feeling the json data

class clsSalesTran
{
    public double paymentcharge { get; set; }
    public double amount { get; set; }
    public long id { get; set; }
    public int shippingcharge { get; set; }
    public int shippingtype { get; set; }
    public int status { get; set; }
    public int paymenttype { get; set; }
    public DateTime date { get; set; }
}


How can I deserialize the above JSON string into List ?

I am using Newtonsoft.Json for deserialize.

解决方案

first create another class:

public class SalesTransactions
{
     public List<clsSalesTran> transactions {get;set;}
     public int count{get;set;}
}

Then use,

JsonConvert.DeserializeObject<SalesTransactions>(inputString)

这篇关于如何反序列化JSON字符串对象名单在C#点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆