如何在 C# dot 中将 json 字符串反序列化为对象列表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14934360/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 13:28:56  来源:igfitidea点击:

How to deserialize json string to object list in c# dot

c#.netjsondesktop-application

提问by G. M. Nazmul Hossain

I am working with the following JSON string

我正在使用以下 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
}


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

我有一个类结构如下用于解析和感受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; }
}


How can I deserialize the above JSON string into List ?

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

I am using Newtonsoft.Jsonfor deserialize.

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

采纳答案by Satpal

first create another class:

首先创建另一个类:

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

Then use,

然后使用,

JsonConvert.DeserializeObject<SalesTransactions>(inputString)

回答by Vikram Bhosale

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();

    }
}

Reply back in case of any issues.

如有任何问题,请回复。

回答by Shibin Sundar

Create a class as below
By creating the list of class 'clsSalesTran' and a variable for 'Count'

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

Note: JsonProperty is mandatory from your Json String

注意:Json 字符串中的 JsonProperty 是必需的

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

Then you may use this class as below to deserialize

然后你可以使用这个类如下反序列化

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

Use the Deserialized object as below

使用反序列化对象如下

double paymentcharge = st.transactions[0].paymentcharge;

回答by Andrew

To deserialize a string to a Listof objects of type clsSalesTran:

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

var myList = JsonConvert.DeserializeObject<List<clsSalesTran>>(inputString);