Why does this anonymous type not deserialize properly using JsonConvert.DeserializeAnonymousType?
There are two problems here, as far as I can tell:
- You don't have a
response
property to deserialize - The "token:123 id:191" part is actually just a string - the value of the outer
token
property
So if you change your code to:
var def = new
{
response = new { token = "" }
};
var deserializedToken = JsonConvert.DeserializeAnonymousType(json, def);
Console.WriteLine(deserializedToken);
then you'll end up with:
{ response = { token = {"token":"123","id":191} } }
If you want to deserialize the token/id part as well, you can do that with:
var innerDef = new { token = "", id = "" };
var deserializedInner = JsonConvert.DeserializeAnonymousType
(deserializedToken.response.token, innerDef);
Console.WriteLine(deserializedInner);
That then prints:
{ token = 123, id = 191 }