Json.Net - 使用“动态”属性反序列化对象

时间:2015-02-19 15:10:57

标签: c# asp.net json serialization json.net

我得到了以下Json数据

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

通过动态JSON,我的意思是Products类上的属性发生了变化,所以我不能在c#类中对它们进行硬编码,就像我使用“HasErrors”一样。

示例:

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

另一个例子:

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

我在.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; }
}

然后我尝试将其反序列化:

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

然后我收到了这个错误:

无法从System.String转换或转换为www.Models.Externals.Product。

[JsonSerializationException:将值“091006”转换为“www.Models.Externals.Product”时出错。路径'Includes.ProductsOrder [0]',第1行,第15173位。]

你们知道我做错了什么吗?

2 个答案:

答案 0 :(得分:1)

我成功地实例化了一个<Response<Product>>类型的对象,并对其进行了序列化以了解引擎盖下发生了什么,作为一个例子,我从中得到的JSON如下:

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

这表明您的JSON根本不与您的对象模型结合。你可以做几件事。将您的JSON格式更改为类似上述内容,或将对象模型更改为以下内容:

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; }
}

答案 1 :(得分:0)

我通过删除ProductRoot类解决了这个问题。

现在看起来像这样:

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; }
}
相关问题