C # JsonConvertDeserialization returns null values

I am trying to understand why I am getting null values ​​for the following:

Json:

{
  "IdentityService": {
    "IdentityTtlInSeconds": "90",
    "LookupDelayInMillis": "3000"
  }
}

Grade:

public class IdentityService
{
    public string IdentityTtlInSeconds { get; set; }

    public string LookupDelayInMillis { get; set; }
}

Called with

  _identityService = JsonConvert.DeserializeObject<IdentityService>(itemAsString);

The class is created, but the values ​​for IdentityTtlInSeconds and LookupDelayInMillis are zero. I do not understand why they should be

+4
source share
1 answer

You need another class - an object with one property IdentityService:

public class RootObject
{
    public IdentityService IdentityService { get; set; }
}

, JSON, , IdentityService, , IdentityTtlInSeconds LookupDelayInMillis. , , JSON.

:

var rootObject = JsonConvert.DeserializeObject<RootObject>(itemAsString);
_identityService = rootObject.IdentityService;
+6

Source: https://habr.com/ru/post/1620980/


All Articles