20

我正在使用以下 JSON 字符串

{
"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
}

我有一个如下的类结构来解析和感受 json 数据

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; }
}

如何将上述 JSON 字符串反序列化为 List ?

我正在使用Newtonsoft.Json进行反序列化。

4

4 回答 4

28

首先创建另一个类:

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

然后使用,

JsonConvert.DeserializeObject<SalesTransactions>(inputString)
于 2013-02-18T10:46:28.220 回答
11

如下创建一个类
通过创建类“clsSalesTran”的列表和一个“Count”的变量

注意:JsonProperty 是您的 Json 字符串中的必填项

public class SalesTransactions
{
     [JsonProperty("transactions")]
     public List<clsSalesTran> transactions {get;set;}
     public int count{get;set;}
}

然后你可以使用下面的这个类来反序列化

SalesTransactions st = JsonConvert.DeserializeObject<SalesTransactions>(inputString)

使用反序列化对象如下

double paymentcharge = st.transactions[0].paymentcharge;
于 2015-12-22T10:32:58.127 回答
3

要将字符串反序列化为 aList类型的对象clsSalesTran

var myList = JsonConvert.DeserializeObject<List<clsSalesTran>>(inputString);
于 2019-05-06T19:24:19.117 回答
-1
class WeapsCollection
{
    public Dictionary<string, WeaponDetails> Weapons { get; set; }

}

class WeaponList
{
    public WeaponDetails AEK { get; set; }
    public WeaponDetails XM8 { get; set; }
}

class WeaponDetails
{
    public string Name { get; set; }
    public int Kills { get; set; }
    public int Shots_Fired { get; set; }
    public int Shots_Hit { get; set; }
}

class Program  
{
    static void Main(string[] args)
    {
        string json = @"
        {
           'weapons':

                    {
                       'aek':
                            {
                               'name':'AEK-971 Vintovka',
                               'kills':47,
                               'shots_fired':5406,
                               'shots_hit':858
                            },
                       'xm8':
                            {
                               'name':'XM8 Prototype',
                               'kills':133,
                               'shots_fired':10170,
                               'shots_hit':1790
                            },
                    }

        }";

        WeapsCollection weps = JsonConvert.DeserializeObject<WeapsCollection>(json);
        Console.WriteLine(weps.Weapons.First().Value.Shots_Fired);            

        Console.ReadLine();

    }
}

如有任何问题,请回复。

于 2013-10-04T12:10:06.137 回答