即使我将 NullValueHandling 设置为 Ignore,C# JsonConvert.DeserializeObject 也会出现 Null 值异常

C# JsonConvert.DeserializeObject giving Null Value Exception even though I have NullValueHandling set to Ignore

我的数据看起来像这样,除了有 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: Error converting value {null} to type 'System.Int32'. Path '455.hitpoints', line 1, position 1597602. ---> System.InvalidCastException: Null object cannot be converted to a value type. at System.Convert.ChangeType(Object value, Type conversionType, IFormatProvider provider) at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.EnsureType(JsonReader reader, Object value, CultureInfo culture, JsonContract contract, Type targetType)

我已经尝试过很多不同的方法来做到这一点。甚至在条目的声明中将其放在生命值之上 class:

[JsonProperty(NullValueHandling = NullValueHandling.Ignore)]

这里是 class:

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)]
}

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

即使您没有提供 class Entry,错误提示您的 Hitpoint 属性 是 int,也就是说,您不能分配 null 对它的价值。您必须将 Hitpoint 定义为 int?.

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

}

更新:

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