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?

4
  • 1
    Where is the question ? Don't post pictures ! Commented Nov 22, 2016 at 9:59
  • 1
    Possible duplicate of How to Convert JSON object to Custom C# object? Commented Nov 22, 2016 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. Commented Nov 22, 2016 at 10:10
  • @MichaelCoxon you should probably point that out on the duplicate if you feel thats true Commented Nov 22, 2016 at 10:25

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Not true, he doesn't care about the metadata, also the names might be case-sensitive.
@HristoYankov Regardless, my point still stands that they need to use the whole JSON object.
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.

1 Comment

Agree, this is how i do it to get the correct model structure.
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; }
    ...
}

Comments

-1
public class SerializedData
{
    public InstagramProfile data { get; set; }
}

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

Comments

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.