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?