即使我将 NullValueHandling 设置为忽略,C# JsonConvert.DeserializeObject 也会给出空值异常

时间:2021-05-20 20:01:20

标签: c# json.net

我的数据看起来像这样,除了有 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:无法将空对象转换为值类型。 在 System.Convert.ChangeType(Object value, Type conversionType, IFormatProvider provider) 在 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)]
}

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

1 个答案:

答案 0 :(得分:1)

即使您没有提供类 Entry,错误也会指出您的 Hitpoint 属性是 int,这意味着您无法为其分配 null 值。您必须将 Hitpoint 定义为 int?

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

}

更新:

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

相关问题