【问题标题】:C# JsonConvert.DeserializeObject giving Null Value Exception even though I have NullValueHandling set to Ignore即使我将 NullValueHandling 设置为 Ignore,C# JsonConvert.DeserializeObject 也会给出 Null 值异常
【发布时间】:2021-05-20 20:01:20
【问题描述】:

我的数据看起来像这样,除了有 1000 个实体,而不仅仅是 2 个。

{
    "1": {
        "id": 1,
        "name": "James",
        "last_updated": "2021-04-26",
        "incomplete": true,
        "valid": true,
        "start_date": "2007-03-20",
        "items": 51,
        "current": 1,
        "hitpoints": 52
    }
    "2": {
        "id": 2,
        "name": "Peter",
        "last_updated": "2021-04-25",
        "incomplete": true,
        "members": true,
        "start_date": "2009-06-13",
        "items": 51,
        "current": 1,
        "hitpoints": 52
    }
}

我的代码是这样的:

try
{
    var settings = new JsonSerializerSettings
    {
        NullValueHandling = NullValueHandling.Ignore,
        MissingMemberHandling = MissingMemberHandling.Ignore
    };
    var dict = JsonConvert.DeserializeObject<Dictionary<string, Entry>>(json, settings);
}
catch (Exception e)
{
    Console.WriteLine(e.ToString()); 
}

这是我得到的错误:

Newtonsoft.Json.JsonSerializationException:将值 {null} 转换为类型“System.Int32”时出错。路径“455.hitpoints”,第 1 行,位置 1597602。---> System.InvalidCastException:Null 对象无法转换为值类型。 在 System.Convert.ChangeType(对象值,类型转换类型,IFormatProvider 提供程序) 在 Newtonsoft.Json.Serialization.JsonSerializerInternalReader.EnsureType(JsonReader reader, Object value, CultureInfoculture, JsonContract contract, Type targetType)

我已经尝试了许多不同的方法来做到这一点。甚至在 Entry 类的声明中将其置于生命值之上:

[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]

这是课程:

public class Entry
{
    public Entry(int id_p, string name_p, string last_updated_p, bool incomplete_p, bool valid_p, string start_date_p, int items_p, int size_p, int hitpoints_p)
    {
        id = id_p;
        name = name_p;
        last_updated = last_updated_p;
        incomplete = incomplete_p;
        valid = valid_p;
        start_date = start_date_p;
        items = items_p;
        size = size_p;
        hitpoints = hitpoints_p;
    }
    
    public int  id                      { get; set; }
    public string   name                { get; set; }
    [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
    public string   last_updated        { get; set; }
    [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
    public bool     incomplete          { get; set; }
    public bool     valid               { get; set; }
    [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
    public string   start_date          { get; set; }
    [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
    public int  items                   { get; set; }
    [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
    public int  current                 { get; set; }
    [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
    public int  hitpoints               { get; set; }
    [JsonProperty(NullValueHandling = NullValueHandling.Ignore)]
}

仍在做同样的事情。有什么想法吗?

【问题讨论】:

  • Entry 类是如何定义的?
  • 另外,可能是粘贴错误,但您的 [JsonProperty(...)] 应该在它相关的属性之前,而不是之后。最后你有一个“悬”。
  • @Nenad 正确,恕我直言,用户应该使用 JsonSerializerSettingsNullValueHandling = NullValueHandling.Ignore 并完全摆脱 [JsonProperty...]
  • [JsonProperty(NullValueHandling = NullValueHandling.Ignore)] 不起作用的事实似乎与您的Entry 类具有参数化构造函数这一事实有关。添加默认构造函数可以解决问题,请参阅dotnetfiddle.net/AwIn0U。有点奇怪,感觉像是 Json.NET 的一个错误。

标签: c# json.net


【解决方案1】:

即使您没有提供类Entry,错误表明您的Hitpoint 属性是int,这意味着您不能为其分配null 值。您必须将Hitpoint 定义为int?

class Entry
{
    //...
    public int? Hitpoint {get; set; }

}

更新:

在您提供的Entry 类中,只有string 已经是Nullable 类型。对于您希望成为“可选”的所有boolint 属性,您应该将类​​型定义更改为bool?int?

【讨论】:

  • 我会试试的。以前从未在有关空条目的任何答案或帖子中看到这一点。谢谢!
  • @TBowman - “将您的值类型转换为可空”是Newtonsoft Json Error converting value {null} to type 'System.Int32' 的公认答案。使您的问题与众不同的是NullValueHandling = NullValueHandling.Ignore(作为同一问题的替代答案)不适用于您的情况。
  • @dbc 是的,我现在看到了这个答案。奇怪的是,当我提出问题时,它没有出现在长长的建议列表中。实际上,当我搜索所有具有正确关键字的建议链接时,我将标题和部分书面问题打开了 30 分钟。这个问题和答案会给我一个明智的建议。
猜你喜欢
  • 2016-02-06
  • 1970-01-01
  • 2021-06-19
  • 2011-07-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多