jake talledo jake talledo - 15 days ago 5
C# Question

Deserialize nested Json String using JsonConvert.DeserializeObject

Hey how do i parse this return Json string. enter image description here

to get only the Data I tried this approach

Model:

public class InstagramProfile
{
public string username { get; set; }
public string bio { get; set; }
public string website { get; set; }
public string profile_picture { get; set; }
public string full_name { get; set; }
public Counts counts { get; set; }
public string id { get; set; }
}


for my Service

public class InstagramService
{
public async Task<InstagramProfile> GetInstagramProfile(string accessToken)
{
var httpClient = new HttpClient();
var userJson = await httpClient.GetStringAsync(Constant.InstagramAu + accessToken);
var instagramProfile = JsonConvert.DeserializeObject<InstagramProfile>(userJson);
return instagramProfile;
}
}


To test

private async Task ExcLog()
{
var intg = new InstagramService();
var token = "MyToken";
var que = await intg.GetInstagramProfile(token);
await DisplayAlert(PageKeys.Tags, que.full_name, "OK");
}


How do I get the Data?

Answer

You need a model that encompasses the whole JSON object..

public class InstagramMeta
{
    public int Code {get;set;}
}

public class InstagramResponse
{
    public InstagramMeta Meta {get;set;}
    public InstagramProfile Data {get;set;}
}

You then deserialize on InstagramResponse.

If you want to only deserialize the data object - ignore the Meta property by removing it from the model.