【问题标题】:Json.Net - Deserialize object with "dynamic" propertiesJson.Net - 使用“动态”属性反序列化对象
【发布时间】:2015-04-20 23:59:17
【问题描述】:

我得到了以下 Json 数据

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

动态 JSON 是指 Products 类的属性发生变化,因此我不能像使用“HasErrors”那样在 c# 类中对它们进行硬编码。

例子:

{
    "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。]

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

【问题讨论】:

  • JSON 动态如何?它不总是遵循这种结构吗?这应该是一个非常简单的 JSON 反序列化。
  • 对不起,是的,它遵循相同的结构,但 productIds 不同,091006 有时可能是 091009 或 091012。所以我不能只是将这些属性添加为 C# 类中的属性,你明白吗?我已经更新了我的帖子。

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


【解决方案1】:

我已经成功地实例化了一个&lt;Response&lt;Product&gt;&gt; 类型的对象并将其序列化以了解幕后发生的事情,例如,我从中得到的 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; }
}

【讨论】:

  • 是的,我在你psoted之前解决了它,但我会给你接受,谢谢伙计!
【解决方案2】:

我通过删除 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; }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-15
    • 1970-01-01
    • 2011-05-30
    • 1970-01-01
    • 2014-08-22
    • 2016-05-31
    • 2014-05-21
    相关资源
    最近更新 更多