Newtonsoft JSON 将零字符串值 ({'name': '0'} 反序列化为 null?

Newtosoft JSON Deserialize zero string value ( {'name': '0'} ) to null?

我在使用值为“0”的字符串 属性 反序列化 JSON 时遇到问题。它被反序列化为 null.

{
  "name":"Some name",
  "isConfigProperty":true,
  "displayProperty":false,
  "default":"0"
}

这是我要反序列化的class:

public class PropertyModel
{
   public string Name { get; set; }   
   public bool IsConfigProperty { get; set; }
   public bool DisplayProperty { get; set; }
   public string Default { get; set; }
}

当我将 Default 值从“0”更改为“1”或任何其他 number/value 时,它会按预期进行反序列化。但是当我把它改成“0”时,我又得到了null

有人知道这件事吗?这可能是 NewtonsoftJson 中的错误还是我遗漏了什么?

谢谢。

您可以使用 JsonConverterAttribute 注释:为 属性.

定义类型
public enum UserStatus
{
    NotConfirmed,
    Active,
    Deleted
}

public class User
{
    public string UserName { get; set; }

    [JsonConverter(typeof(StringEnumConverter))]
    public UserStatus Status { get; set; }
}

Doc

已解决。 Visual Studio 调试器中一定发生了一些奇怪的事情,或者也许我只是不知道(我是 ASP.NET/Core/Microsoft 中的新手)有时或在某些情况下调试 Blazor webassembly 时看起来我不知道某些变量在调试时显示为空值......实际上它们不是空值。 我在实现记录到文件时发现了这一点,并且“默认”:“0”是 0 而不是 null。

也感谢您提供的所有答案,很抱歉在我尝试所有解决方案之前造成了这个问题。