Deserializing nested json data

It's not entirely straightforward to deserialize the following data


The problem is - it's one level nested down. You can also adopt a similar structure in your class models but it's pretty useless to do that. So, what's an alternative? Following four lines:


var content = await response.Content.ReadAsStringAsync();
JObject parent = JObject.Parse(content); 
var root = parent.Value<JObject>("data"); 
var fbToken = JsonConvert.DeserializeObject<FbToken>(root.ToString());

JObject.Parse from Newtonsoft comes to rescue. 

We specify the parent\shell node like this - parent.Value<JObject>("data"). It helps in getting past the enclosure.

Here is FbToken class -

public class FbToken
public string AppId { get; set; }

public string Type { get; set; }

public string Application { get; set; }

public long ExpiresAt { get; set; }

public bool IsValid { get; set; }

public string[] Scopes { get; set; }

public String UserId { get; set; }


Happy Deserializing.



Reference -


Add comment