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

Roc*_*cks 1 c# json json.net

大家好我有一个类似下面的JSON

{
  "totals": {
    "tokenType": "string",
    "tokenDenomination": "double",
    "count": "int"
  },
  "IDCode": "string",
  "Key": "string"
}
Run Code Online (Sandbox Code Playgroud)

和c#代码反序列化到对象是

internal class GetTokenRootInfo
{
    public  static Totals totals{ get; set;}
    public  static string IDCode{ get; set;}
    public  static string Key{ get; set;}
}
Run Code Online (Sandbox Code Playgroud)

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

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

任何人都可以告诉我在反序列化对象时静态类型不起作用的原因吗?

Bri*_*ers 5

如果您确实要反序列化类上的静态成员,可以使用[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; }
}
Run Code Online (Sandbox Code Playgroud)