0

I know hot to Deserialize JSONs like { "key1": "value1", "key2": "value2"}

But now I am trying to use instagram API and it returns me a Json in this form

{
    "access_token": "fb2e77d.47a0479900504cb3ab4a1f626d174d2d",
    "user": {
        "id": "1574083",
        "username": "snoopdogg",
        "full_name": "Snoop Dogg",
        "profile_picture": "..."
    }
}

Since it is not structured like an array of keys and values I am confused. I need to assign those values to the following class of mine.

public class AccessTokenResult
        {
            public string AccessTokenString { get; set; }

            public class UserWhoTook
            {
                public int ID { get; set; }
                public string UserName { get; set; }
                public string FullName { get; set; }
            }
        }
zgrkpnr89
  • 325
  • 1
  • 6
  • 14

3 Answers3

2

Checkout JsonConvert and questions like Using JsonConvert.DeserializeObject to deserialize Json to a C# POCO class

Community
  • 1
  • 1
Robert Fricke
  • 3,499
  • 17
  • 32
  • I am upvoting it because I also had a problem to build the structure. But this was helpful, and really clear. – zgrkpnr89 Aug 03 '15 at 15:27
0

I reccomend you use http://json2csharp.com/ to format your Objects. Just paste in the json and it should spit out the classes for you.

For example, the json you posted generated.

public class User
{
    public string id { get; set; }
    public string username { get; set; }
    public string full_name { get; set; }
    public string profile_picture { get; set; }
}

public class RootObject
{
    public string access_token { get; set; }
    public User user { get; set; }
}
Andres Castro
  • 1,669
  • 13
  • 15
0

You can use third party libraries for JSON, for example Newton Json.net: https://json.codeplex.com/

nikolai.serdiuk
  • 602
  • 8
  • 11