Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

deserializing Static properties using json.net?

Tags:

json

c#

json.net

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?

like image 722
RockyRocks Avatar asked Mar 27 '26 13:03

RockyRocks


1 Answers

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; }
}
like image 131
Brian Rogers Avatar answered Mar 29 '26 03:03

Brian Rogers



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!