Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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.

like image 809
G. M. Nazmul Hossain Avatar asked Feb 18 '13 10:02

G. M. Nazmul Hossain


People also ask

How do I deserialize JSON?

A common way to deserialize JSON is to first create a class with properties and fields that represent one or more of the JSON properties. Then, to deserialize from a string or a file, call the JsonSerializer. Deserialize method.

How do you serialize and deserialize an object in C# using JSON?

In Deserialization, it does the opposite of Serialization which means it converts JSON string to custom . Net object. In the following code, it calls the static method DeserializeObject() of the JsonConvert class by passing JSON data. It returns a custom object (BlogSites) from JSON data.


1 Answers

first create another class:

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

Then use,

JsonConvert.DeserializeObject<SalesTransactions>(inputString)
like image 136
Satpal Avatar answered Sep 21 '22 09:09

Satpal