0

I am unable to correctly transform the JSON response to a class and then display in in a string.

This is the response:

    {
    "productBaseInfo": {
        "productIdentifier": {
            "productId": "SHODRYT32JN5N6WY",
            "categoryPaths": {
                "categoryPath": [
                    [
                        {
                            "title": "Footwear"
                        },
                        {
                            "title": "Men's Footwear"
                        },
                        {
                            "title": "Shoes"
                        },
                        {
                            "title": "Casual Shoes"
                        }
                    ]
                ]
            }
        },
        "productAttributes": {
            "title": "Diesel Magnete Exposure I Boots",
            "productDescription": null,
            "imageUrls": {
                "400x400": "url",
                "75x75": "url",
                "700x700": "url",
                "275x275": "url",
                "125x125": "url",
                "40x40": "url",
                "100x100": "url",
                "200x200": "url",
                "unknown": "http://img5a.flixcart.com/image/shoe/6/w/y/t7434-magnete-exposure-i-sneaker-mid-diesel-43-original-imadtxfxcgzkbzbg.jpeg",
                "180x240": "url",
                "275x340": "url"
            },
            "maximumRetailPrice": {
                "amount": 15800,
                "currency": "INR"
            },
            "sellingPrice": {
                "amount": 15800,
                "currency": "INR"
            },
            "productUrl": "http://dl.flipkart.com/dl/diesel-magnete-exposure-boots/p/itmdryt4zdzbwehx?pid=SHODRYT32JN5N6WY&affid=keviv2809",
            "productBrand": "Diesel",
            "inStock": true,
            "codAvailable": true,
            "emiAvailable": false,
            "discountPercentage": 0,
            "cashBack": null,
            "offers": [
                {
                    "title": "Get Flat 60% Off on this product"
                }
            ],
            "size": "44",
            "color": "Grey",
            "sizeUnit": "Euro",
            "sizeVariants": "[SHODRYT33YFFZHTU, SHODRYT3BZGFSBNV, SHODRYT3QECRQZ8Y, SHODRYT3QPQZG4XF, SHODRYT3Y5SUNHRM]",
            "colorVariants": "[SHODRYT3MVHPHVS2, SHODVN2YP5AXADT9, SHODVN2YUGUUGUST]",
            "styleCode": "MAGNETE EXPOSURE I - sneaker mid"
        }
    },
    "productShippingBaseInfo": {
        "shippingOptions": [
            {
                "estimatedDelivery": 3,
                "deliveryTimeUnits": "DAYS",
                "shippingType": "REGULAR"
            },
            {
                "estimatedDelivery": 1,
                "deliveryTimeUnits": "DAYS",
                "shippingType": "REGULAR"
            },
            {
                "estimatedDelivery": 0,
                "deliveryTimeUnits": "DAYS",
                "shippingType": "REGULAR"
            }
        ]
    },
    "offset": "v1:osp-cil-nit-e1f:SHODRYT32JN5N6WY"
}

The classes used by me are:

public class CategoryPaths
{
    public List<ProductIdentifier> categoryPath { get; set; }
}

public class ProductIdentifier
{
    public string productId { get; set; }
    public CategoryPaths categoryPaths { get; set; }
}

public class ImageUrls
{
    public string 400x400 { get; set; }
    public string 125x167 { get; set; }
    public string 75x75 { get; set; }
    public string 700x700 { get; set; }
    public string 275x275 { get; set; }
    public string 125x125 { get; set; }
    public string 40x40 { get; set; }
    public string 100x100 { get; set; }
    public string 200x200 { get; set; }
    public string 1100x1360 { get; set; }
    public string unknown { get; set; }
    public string 180x240 { get; set; }
    public string 275x340 { get; set; }
}

public class MaximumRetailPrice
{
    public double amount { get; set; }
    public string currency { get; set; }
}

public class SellingPrice
{
    public double amount { get; set; }
    public string currency { get; set; }
}

public class Offer
{
    public string title { get; set; }
}

public class ProductAttributes
{
    public string title { get; set; }
    public object productDescription { get; set; }
    public ImageUrls imageUrls { get; set; }
    public MaximumRetailPrice maximumRetailPrice { get; set; }
    public SellingPrice sellingPrice { get; set; }
    public string productUrl { get; set; }
    public string productBrand { get; set; }
    public bool inStock { get; set; }
    public bool codAvailable { get; set; }
    public bool emiAvailable { get; set; }
    public double discountPercentage { get; set; }
    public object cashBack { get; set; }
    public List<Offer> offers { get; set; }
    public string size { get; set; }
    public string color { get; set; }
    public string sizeUnit { get; set; }
    public string sizeVariants { get; set; }
    public string colorVariants { get; set; }
    public string styleCode { get; set; }
}

public class ProductBaseInfo
{
    public ProductIdentifier productIdentifier { get; set; }
    public ProductAttributes productAttributes { get; set; }
}

public class ShippingOption
{
    public int estimatedDelivery { get; set; }
    public string deliveryTimeUnits { get; set; }
    public string shippingType { get; set; }
}

public class ProductShippingBaseInfo
{
    public List<ShippingOption> shippingOptions { get; set; }
}

public class RootObject
{
    public ProductBaseInfo productBaseInfo { get; set; }
    public ProductShippingBaseInfo productShippingBaseInfo { get; set; }
    public string offset { get; set; }
}

I am using the following code to deserialize it and then a foreach loop to display it.

CategoryPaths pi = new System.Web.Script.Serialization.JavaScriptSerializer().Deserialize<CategoryPaths>(json);

I am getting the following error:

object reference not set to an instance of an object.

I think I am going wrong in using the Deserialize(method) Someone please help

[Edit]: I got the Deserialize method correct now by using the foll code:

var dynJson = JsonConvert.DeserializeObject<RootObject>(json); Label1.Text = dynJson.productBaseInfo.productAttributes.maximumRetailPrice.amount.ToString()

I am unable to store the json response with the value "400X400":"url" in a string as it is not allowing me to declare string variable with the name 400X400

1 Answers1

0

The type defined does not match with the Json Structure. You can use the below code to dynamically deserialize and get an idea of the structure

using System.Web.Script.Serialization;

string json = "{\"productBaseInfo\":{\"productIdentifier\":{\"productId\":\"SHODRYT32JN5N6WY\",\"categoryPaths\":{\"categoryPath\":[[{\"title\":\"Footwear\"},{\"title\":\"Men's Footwear\"},{\"title\":\"Shoes\"},{\"title\":\"Casual Shoes\"}]]}},\"productAttributes\":{\"title\":\"Diesel Magnete Exposure I Boots\",\"productDescription\":null,\"imageUrls\":{\"400x400\":\"\",\"url\":\"url\",\"75x75\":\"url\",\"700x700\":\"url\",\"275x275\":\"url\",\"125x125\":\"url\",\"40x40\":\"url\",\"100x100\":\"url\",\"200x200\":\"\",\"url\":\"url\",\"unknown\":\"http://img5a.flixcart.com/image/shoe/6/w/y/t7434-magnete-exposure-i-sneaker-mid-diesel-43-original-imadtxfxcgzkbzbg.jpeg\",\"180x240\":\"url\",\"275x340\":\"url\"},\"maximumRetailPrice\":{\"amount\":15800.0,\"currency\":\"INR\"},\"sellingPrice\":{\"amount\":15800.0,\"currency\":\"INR\"},\"productUrl\":\"http://dl.flipkart.com/dl/diesel-magnete-exposure-boots/p/itmdryt4zdzbwehx?pid=SHODRYT32JN5N6WY&affid=keviv2809\",\"productBrand\":\"Diesel\",\"inStock\":true,\"codAvailable\":true,\"emiAvailable\":false,\"discountPercentage\":0.0,\"cashBack\":null,\"offers\":[{\"title\":\"Get Flat 60% Off on this product\"}],\"size\":\"44\",\"color\":\"Grey\",\"sizeUnit\":\"Euro\",\"sizeVariants\":\"[SHODRYT33YFFZHTU, SHODRYT3BZGFSBNV, SHODRYT3QECRQZ8Y, SHODRYT3QPQZG4XF, SHODRYT3Y5SUNHRM]\",\"colorVariants\":\"[SHODRYT3MVHPHVS2, SHODVN2YP5AXADT9, SHODVN2YUGUUGUST]\",\"styleCode\":\"MAGNETE EXPOSURE I - sneaker mid\"}},\"productShippingBaseInfo\":{\"shippingOptions\":[{\"estimatedDelivery\":3,\"deliveryTimeUnits\":\"DAYS\",\"shippingType\":\"REGULAR\"},{\"estimatedDelivery\":1,\"deliveryTimeUnits\":\"DAYS\",\"shippingType\":\"REGULAR\"},{\"estimatedDelivery\":0,\"deliveryTimeUnits\":\"DAYS\",\"shippingType\":\"REGULAR\"}]},\"offset\":\"v1:osp-cil-nit-e1f:SHODRYT32JN5N6WY\"}";

dynamic value = new JavaScriptSerializer().DeserializeObject(json);
Rad
  • 391
  • 4
  • 14
  • Thanks for your response sir. I used the below code and it worked: var dynJson = JsonConvert.DeserializeObject(json); Label1.Text = dynJson.productBaseInfo.productAttributes.maximumRetailPrice.amount.ToString(); The api response that I am getting is dynamic and hence I cannot specify the json string explicitly sir. What I am stuck on right now is that the image url returned in the json format is of the form: "400x400":"someurl" I am unable to declare a string type with the name 400x400 and hence not able to store the value of the Url. I Can't change the json string as mentioned. – Vivek Venugopalan Apr 24 '15 at 13:57
  • sorry that was a typo error from me. Now edited. My main question is how can I declare a string variable with the name 400X400. The json response has the name 400X400. I am able to get the value of one url from the json response with the name unknown as I can declare a string named unknown and store the value in it. – Vivek Venugopalan Apr 27 '15 at 06:17
  • you can use the link http://json2csharp.com/ to convert the json into c# class. You can use the same class structure to deserialize the data. You can use json.Net library to deserialize Json into strongly typed data. var obj = JsonConvert.DeserializeObject(json); – Rad Apr 27 '15 at 06:36