0

I was tried to deserialise this json string to c# objects but i was not succeeded.can anone one help me out how can i acheive this.

    {
    products_info: [10]
    0:  {
    product_id: "20"
    product_name: "Crocin"
    medical_name: "Crocin"
    slot_no: "1"
    num_of_units: "10"
    price: "16"
    expiry_date: "2016-01-15"
    product_image: "http://xyz/prdct_imgs/eno.png"
    }-

    ..................

    9:  {
    product_id: "29"
    product_name: "Stayfree"
    medical_name: "Stayfree"
    slot_no: "11"
    num_of_units: "10"
    price: "30"
    expiry_date: "2016-09-02"
    product_image: "http://xyz/prdct_imgs/eno.png"
    }-
    -
    response: "1"
    }
  • 6
    what have you tried? what is not working? we only see the json serialized data here: how do you try to create a corresponding c# object? – Gian Paolo Oct 18 '15 at 10:34
  • You can take a look at this post: http://stackoverflow.com/questions/4611031/convert-json-string-to-c-sharp-object – Jesse Oct 18 '15 at 11:33

1 Answers1

2

The Json you've provided seems to be in wrong format (please check the rules of JSON format), here's how it should look:

{
    "products_info": [
        {
            "product_id": "20",
            "product_name": "Crocin",
            "medical_name": "Crocin",
            "slot_no": "1",
            "num_of_units": "10",
            "price": "16",
            "expiry_date": "2016-01-15",
            "product_image": "http://xyz/prdct_imgs/eno.png"
        },
        {
            "product_id": "29",
            "product_name": "Stayfree",
            "medical_name": "Stayfree",
            "slot_no": "11",
            "num_of_units": "10",
            "price": "30",
            "expiry_date": "2016-09-02",
            "product_image": "http://xyz/prdct_imgs/eno.png"
        }
    ],
    "response": "1"
}

Now when you have the right format you can deserialize using Json.net for example (you can install it from nuget) to the following class:

public class ProductsInfo
{
    public string product_id { get; set; }
    public string product_name { get; set; }
    public string medical_name { get; set; }
    public string slot_no { get; set; }
    public string num_of_units { get; set; }
    public string price { get; set; }
    public string expiry_date { get; set; }
    public string product_image { get; set; }
}

public class YourClass
{
    public List<ProductsInfo> products_info { get; set; }
    public string response { get; set; }
}

YourClass yourClass = JsonConvert.DeserializeObject(json);

You can see how it's done here: http://www.newtonsoft.com/json/help/html/deserializeobject.htm

Felix Av
  • 1,254
  • 1
  • 14
  • 22