Search code examples
c#jsonjson.netjson-deserialization

Creating datamodel from JSON


I am working on a winform application that is going to communicate with a webstore API. When I try to get all products from the webstore they present me with the following JSON:

{
"code":200,
"data":{
    "product_data":{
        "122":{
            "products_id":"122",
            "products_name":"Camilla armchair",
            "products_model":"",
            "products_url":"http:\/\/mystore-demo.no\/products\/camilla-armchair",
            "products_url_identifier":"camilla-armchair",
            "products_sort_order":"0",
            "products_quantity":"0",
            "products_weight":"0",
            "products_status":"1",
            "products_price_ex_tax":"249.0000",
            "products_tax_percentage":"25",
            "products_description":"Beskrivelse Camilla armchair",
            "products_date_added":"2011-10-12 15:32:06",
            "products_last_modified":"2011-10-12 15:32:06",
            "products_brand_name":"",
            "products_brand_id":"0",
            "products_categories":[
                "42",
                "44"
            ],
            "products_attributes":[

            ],
            "products_tabs":[

            ],
            "products_images":[
                "http:\/\/mystore-demo.no\/users\/demo_mystore_no\/images\/122_Camilla_armchair_1.jpg"
            ],
            "products_index": "2"
        },
        "123":{
            "products_id":"123",
            "products_name":"Egg Chair",
            "products_model":"",
            "products_url":"http:\/\/mystore-demo.no\/products\/egg-chair",
            "products_url_identifier":"egg-chair",
            "products_sort_order":"0",
            "products_quantity":"0",
            "products_weight":"0",
            "products_status":"1",
            "products_price_ex_tax":"2999.0000",
            "products_tax_percentage":"25",
            "products_description":"Beskrivelse Egg Chair",
            "products_date_added":"2011-10-12 15:33:27",
            "products_last_modified":"2011-10-12 15:33:27",
            "products_brand_name":"",
            "products_brand_id":"0",
            "products_categories":[
                "42"
            ],
            "products_attributes":[

            ],
            "products_tabs":[

            ],
            "products_images":[
                "http:\/\/mystore-demo.no\/users\/demo_mystore_no\/images\/123_Egg_Chair_1.jpg"
            ],
            "products_index": "3"
        },
        "121":{
            "products_id":"121",
            "products_name":"Round chair",
            "products_model":"",
            "products_url":"http:\/\/mystore-demo.no\/products\/round-chair",
            "products_url_identifier":"round-chair",
            "products_sort_order":"0",
            "products_quantity":"0",
            "products_weight":"0",
            "products_status":"1",
            "products_price_ex_tax":"1599.0000",
            "products_tax_percentage":"25",
            "products_description":"Beskrivelse Round Chair",
            "products_date_added":"2011-10-11 10:43:42",
            "products_last_modified":"2011-10-11 10:43:42",
            "products_brand_name":"",
            "products_brand_id":"0",
            "products_categories":[
                "42"
            ],
            "products_attributes":[

            ],
            "products_tabs":[

            ],
            "products_images":[
                "http:\/\/mystore-demo.no\/users\/demo_mystore_no\/images\/121_Round_chair_1.jpg"
            ]
            ,
            "products_index": "1"
        }
    },
    "product_count_total":3
}

}

I have created a MyStoreResponse class (datamodel) that looks like this:

using Newtonsoft.Json;
namespace MyStoreSync.Models
{
public class Product
{
    [JsonProperty("products_id")]
    public string ProductsId { get; set; }

    [JsonProperty("products_name")]
    public string ProductsName { get; set; }

    [JsonProperty("products_model")]
    public string ProductsModel { get; set; }

    [JsonProperty("products_url")]
    public string ProductsUrl { get; set; }

    [JsonProperty("products_url_identifier")]
    public string ProductsUrlIdentifier { get; set; }

    [JsonProperty("products_sort_order")]
    public string ProductsSortOrder { get; set; }

    [JsonProperty("products_quantity")]
    public string ProductsQuantity { get; set; }

    [JsonProperty("products_weight")]
    public string ProductsWeight { get; set; }

    [JsonProperty("products_status")]
    public string ProductsStatus { get; set; }

    [JsonProperty("products_price_ex_tax")]
    public string ProductsPriceExTax { get; set; }

    [JsonProperty("products_tax_percentage")]
    public string ProductsTaxPercentage { get; set; }

    [JsonProperty("products_description")]
    public string ProductsDescription { get; set; }

    [JsonProperty("products_date_added")]
    public string ProductsDateAdded { get; set; }

    [JsonProperty("products_last_modified")]
    public string ProductsLastModified { get; set; }

    [JsonProperty("products_brand_name")]
    public string ProductsBrandName { get; set; }

    [JsonProperty("products_brand_id")]
    public string ProductsBrandId { get; set; }

    [JsonProperty("products_categories")]
    public string[] ProductsCategories { get; set; }

    [JsonProperty("products_attributes")]
    public object[] ProductsAttributes { get; set; }

    [JsonProperty("products_tabs")]
    public object[] ProductsTabs { get; set; }

    [JsonProperty("products_images")]
    public string[] ProductsImages { get; set; }

    [JsonProperty("products_index")]
    public string ProductsIndex { get; set; }
}

public class ProductData
{
    [JsonProperty("Product")]
    public Product Product { get; set; }
}

public class Data
{
    [JsonProperty("product_data")]
    public ProductData ProductData { get; set; }

    [JsonProperty("product_count_total")]
    public int ProductCountTotal { get; set; }
}

public class MyStoreResponse
{
    [JsonProperty("code")]
    public int Code { get; set; }

    [JsonProperty("data")]
    public Data Data { get; set; }
}

}

I get the correct ProductCountTotal, but Product is always null. Can anyone with more knowledege about parsing JSON help me?


Solution

  • You need to make the ProductData property be a dictionary:

    public class Data
    {
        [JsonProperty("product_data")]
        public Dictionary<string, Product> ProductData { get; set; }
    
        [JsonProperty("product_count_total")]
        public int ProductCountTotal { get; set; }
    }
    

    The ProductData class is unnecessary. The JSON property names "122", "123" and "121" become the dictionary keys, and the associated Product the value. See Serialize a Dictionary.