4

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

Json:

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

Class:

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

    public string LookupDelayInMillis { get; set; }
}

Called with :

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

The class is instantiated but the values for IdentityTtlInSeconds and LookupDelayInMillis are null. I cannot see why they should be

1
  • Deserialize to dynamic instead of your class. see what it deserializes to and make sure your class is like that. Commented Dec 20, 2015 at 13:48

1 Answer 1

6

You need one more class - an object which has one property called IdentityService:

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

You need this class because JSON that you have has one property called IdentityService, and this object has two properties, called IdentityTtlInSeconds and LookupDelayInMillis. If you are using a default serializer your classes need to reflect the structure that you have in your JSON string.

And now you can use it to deserialize your string:

var rootObject = JsonConvert.DeserializeObject<RootObject>(itemAsString);
_identityService = rootObject.IdentityService;
Sign up to request clarification or add additional context in comments.

1 Comment

Or remove IdentityService from the json so that the data contains only the properties and you can deserialize directly to an IdentityService.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.