Twiss
Twiss

Reputation: 13

how to access nested object in JSON string

I am getting an API response in JSON format as follows:

{ 
  "token_type":"Bearer",
  "access_token":"12345678910",
  "user":{ 
           "id":123456,
           "username":"jbloggs",
           "resource":2,
           "firstname":"joe"
         }
}

dynamic usrdetail = JsonConvert.DeserializeObject(JSONString);

I can use usrdetail to access token_type and access_token (usrdetail.access_token) but how do I reach the user information?

I have tried usrdetail.user.id but that doesnt work?

Thanks G

Upvotes: 0

Views: 1730

Answers (4)

Cerfy
Cerfy

Reputation: 36

JSON objects are written in key/value pairs. Therefore, to access JSON objects, you can use the square brackets and place the key in it.

So for your example, you can do usrdetail["user"]["id"] which should retrieve the user's id.

Upvotes: 2

smolchanovsky
smolchanovsky

Reputation: 1863

This should work.

var jsonStr = "{ \"token_type\":\"Bearer\",\"access_token\":\"12345678910\",\"user\":{\"id\":123456,\"username\":\"jbloggs\",\"resource\":2,\"firstname\":\"joe\"}}";
dynamic jsonObject = JsonConvert.DeserializeObject(jsonStr);
int userId = jsonObject.user.id;
Console.WriteLine(userId);

See this: https://dotnetfiddle.net/huKNpU

Upvotes: 1

User7291
User7291

Reputation: 1115

you can serialize your dynamic object dynamic usrdetail and then deserialize it to a predefined object like below:

 dynamic usrdetail = JsonConvert.DeserializeObject(JSONString);
 var jsonParam = JsonConvert.SerializeObject(usrdetail);
 PredefiendClass obj = JsonConvert.DeserializeObject<PredefiendClass>(jsonParam);

Upvotes: 0

er-sho
er-sho

Reputation: 9771

1) Create quick type for your json string from json2csharp

public class User
{
    public int id { get; set; }
    public string username { get; set; }
    public int resource { get; set; }
    public string firstname { get; set; }
}

public class Token
{
    public string token_type { get; set; }
    public string access_token { get; set; }
    public User user { get; set; }
}

2) Then Deserialize your json into above quick type like

class Program
{
    static void Main(string[] args)
    {
        string json = @"{ 'token_type':'Bearer','access_token':'12345678910','user':{ 
                                       'id':123456,'username':'jbloggs','resource':2,'firstname':'joe'}
                                     }";


        Token token = JsonConvert.DeserializeObject<Token>(json);

        Console.WriteLine("token_type: " + token.token_type);
        Console.WriteLine("access_token: " + token.access_token);
        Console.WriteLine();
        Console.WriteLine("id: " + token.user.id);
        Console.WriteLine("username: " + token.user.username);
        Console.WriteLine("resource: " + token.user.resource);
        Console.WriteLine("firstname: " + token.user.firstname);

        Console.ReadLine();
    }
}

Output:

enter image description here

Upvotes: 2

Related Questions