1

I got the following Json data

{
    "HasErrors": false,
    "Includes": {
        "Products": {
            "091006": { 
               "hej" : "tja"
             },
             "091026": { 
               "hej" : "tjafsafsa"
             }
         }
    }   
}

By dynamic JSON i mean that the propertys on the Products class changes so I cant hardcode them in the c# class in the same way that I do with "HasErrors" for example.

Example:

{
    "HasErrors": false,
    "Includes": {
        "Products": {
            "091006": { 
               "hej" : "tja"
             },
             "091026": { 
               "hej" : "tjafsafsa"
             }
         }
    }   
}

Another example:

{
    "HasErrors": false,
    "Includes": {
        "Products": {
            "091126": { //CHANGED 
               "hej" : "tja"
             },
             "091043226": { //CHANGED
               "hej" : "tjafsafsa"
             }
         }
    }   
}

I've built up the following classes in .NET

Response.cs

public class Response<T> where T : new()
{
    [JsonProperty("hasErrors")]
    public bool HasErrors { get; set; }

    [JsonProperty("includes")]
    public Includes<T> Includes { get; set; }
}

Includes.cs

public class Includes<T> where T : new()
{
    [JsonProperty("products")]
    public ProductRoot Products { get; set; }
}

ProductRoot.cs

public class ProductRoot
{
    [JsonProperty("products")]
    public Dictionary<string, Product> Products { get; set; } 
}

Product.cs

public class Product
{
    [JsonProperty("hej")]
    public string Hej { get; set; }
}

I then try to Deserialize it like this:

var hej = JsonConvert.DeserializeObject<Response<Product>>(json_from_above_as_string);

And then I get this error:

Could not cast or convert from System.String to www.Models.Externals.Product.

[JsonSerializationException: Error converting value "091006" to type 'www.Models.Externals.Product'. Path 'Includes.ProductsOrder[0]', line 1, position 15173.]

You guys have any idea of what Im doing wrong?

2
  • How is the JSON dynamic? Does it not always follow that structure? This should be a pretty simple bit of JSON to deserialize. Commented Feb 19, 2015 at 15:14
  • Ah sorry, yes it follows the same structure, but the productIds differ, the 091006 could be 091009 or 091012 sometimes. So I cant just add theses properties as properties in the C# class, do you understand me? I've updated my post. Commented Feb 19, 2015 at 15:17

2 Answers 2

1

I've instantiated an object of type <Response<Product>> successfully and serialized it to get a feel about what's happening under the hood and, as an example, the JSON I get out of it is as follows:

{  
    "hasErrors":false,
    "includes":{  
        "products":{  
            "products":{  
                "091006":{  
                    "hej":"tja"
                }
            }
        }
    }
}

This shows that your JSON simply doesn't marry up with your object model. You can do a couple of things. Either change your JSON format to something like the above, or change your object model to the following:

public class Response<T> where T : new()
{
    [JsonProperty("hasErrors")]
    public bool HasErrors { get; set; }

    [JsonProperty("includes")]
    public Includes<T> Includes { get; set; }
}

public class Includes<T> where T : new()
{
    [JsonProperty("products")]
    public Dictionary<string, T> Products { get; set; }
}

public class Product
{
    [JsonProperty("hej")]
    public string Hej { get; set; }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Yeah I solved it just before you psoted, but I will give you the accept, thanks man!
0

I solved it by removing the ProductRoot class.

It now looks like this:

public class Response<T> where T : new()
{
    [JsonProperty("hasErrors")]
    public bool HasErrors { get; set; }

    [JsonProperty("includes")]
    public Includes<T> Includes { get; set; }
}

public class Includes<T> where T : new()
{
    [JsonProperty("products")]
    public Dictionary<string, Product>Products { get; set; }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.