0

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?

jake talledo
  • 610
  • 11
  • 24
  • 1
    Where is the question ? Don't post pictures ! – mybirthname Nov 22 '16 at 09:59
  • 1
    Possible duplicate of [How to Convert JSON object to Custom C# object?](http://stackoverflow.com/questions/2246694/how-to-convert-json-object-to-custom-c-sharp-object) – Liam Nov 22 '16 at 10:02
  • @Liam I feel that the accepted answer on the linked duplicate post - while right - is a bit of an antipattern regarding POCO classes. I find it is better to use a service to do the deserialization like OP has demonstrated. – Michael Coxon Nov 22 '16 at 10:10
  • @MichaelCoxon you should probably point that out on the duplicate if you feel thats true – Liam Nov 22 '16 at 10:25

4 Answers4

2

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.

Michael Coxon
  • 5,311
  • 1
  • 24
  • 51
1

Try generating your model with http://json2csharp.com/
After that you can deserialize your json to a root object. From that you can access the data field and process it.

Mitulát báti
  • 2,086
  • 5
  • 23
  • 37
1

To build on the awnser from Michael Coxon you can also add the JsonProperty Attribute to map the json names to a property name that you then can rename to what fits your naming convention best:

public class InstagramProfile
{
    [JsonProperty("username")]
    public string Username { get; set; }
    [JsonProperty("bio")]
    public string Bio { get; set; }
    [JsonProperty("website")]
    public string Website { get; set; }
    [JsonProperty("profile_picture")]
    public string ProfilePicture { get; set; }
    [JsonProperty("full_name")]
    public string FullName { get; set; }
    ...
}
Threezool
  • 453
  • 4
  • 11
-1
public class SerializedData
{
    public InstagramProfile data { get; set; }
}

Deserialize to SerializedData. Then your data will be in .data

hyankov
  • 4,049
  • 1
  • 29
  • 46