使用 json.net 反序列化静态属性?

2024-04-04

大家好,我有一个如下所示的 JSON

{
  "totals": {
    "tokenType": "string",
    "tokenDenomination": "double",
    "count": "int"
  },
  "IDCode": "string",
  "Key": "string"
}

反序列化为对象的 C# 代码是

internal class GetTokenRootInfo
{
    public  static Totals totals{ get; set;}
    public  static string IDCode{ get; set;}
    public  static string Key{ get; set;}
}

当我使用jsonconvert.DeserializeObject<gettokenrootinfo>(json);没有设置任何内容,每个 var 都是 null。

但如果我删除静态类型,那么一切都会正常。

谁能告诉我反序列化对象时静态类型不起作用的原因吗?


如果您确实想反序列化为类上的静态成员,您可以用[JsonProperty]属性,这将允许它工作:

internal class GetTokenRootInfo
{
    [JsonProperty("totals")]
    public static Totals totals { get; set; }
    [JsonProperty("IDCode")]
    public static string IDCode { get; set; }
    [JsonProperty("Key")]
    public static string Key { get; set; }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 json.net 反序列化静态属性? 的相关文章

随机推荐