deserializing Static properties using json.net?

陌路散爱 提交于 2019-12-11 09:27:50

问题


Hi guys I have a JSON like the one below

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

and c# code to deserialize in to object is

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

When I use jsonconvert.DeserializeObject<gettokenrootinfo>(json); nothing is being set and every var is a null.

But if I remove static types then everything works.

Can any one tell me the reason why static types are not working when deserializing the object?


回答1:


If you really want to deserialize to static members on the class, you can explicitly mark them with a [JsonProperty] attribute, and that will allow it to work:

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; }
}


来源:https://stackoverflow.com/questions/22605529/deserializing-static-properties-using-json-net

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!