How to deserialize json string to object list in c# dot
Asked Answered
S

4

23

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.

Sadowski answered 18/2, 2013 at 10:41 Comment(0)
H
31

first create another class:

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

Then use,

JsonConvert.DeserializeObject<SalesTransactions>(inputString)
Habitual answered 18/2, 2013 at 10:46 Comment(7)
Is the count property necessary? Not in my experience.Polyclinic
@gusmallysupportsMonica, Wheather the count property is necessary, is not relevant to question. The objective of the question was deserialization of incoming JSON stringHabitual
right, but if I had asked this question, I would assume that your answer would give what was required and nothing more. As we know, it's good practice to omit needless code. FWIW I upvoted your question, but left a comment for subsequent visitors who may be wondering the same thing.Polyclinic
isnt it more cleaner to do JsonConvert.DeserializeObject<List<SalesTransactions>>(inputString) then you won't need the extra class ?Nayarit
@Jessedegans Do note that List<clsSalesTran> is only the inner list of the whole JSON object. JsonConvert.DeserializeObject<List<SalesTransactions>>(inputString) would only work if the input string is only the listJohnettajohnette
@gusmallysupportsMonica you might have missed the "count" property at the end of the json.Changeling
@Changeling you're right; I totally did. Sorry Satpal I misunderstood what you were saying.Polyclinic
S
12

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

Note: JsonProperty is mandatory from your Json String

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;
Slemmer answered 22/12, 2015 at 10:32 Comment(0)
A
8

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

var myList = JsonConvert.DeserializeObject<List<clsSalesTran>>(inputString);
Aprylapse answered 6/5, 2019 at 19:24 Comment(1)
Note: this will not work for the JSON in the original question. This will only work if the entirety of the JSON being parsed in inputString is only an unnamed array.Chitterlings
E
-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();

    }
}

Reply back in case of any issues.

Englishman answered 4/10, 2013 at 12:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.