0

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

4 Answers 4

2

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.

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

1 Comment

This is a good 'quick and dirty' solution, but consider refactoring to something more sustainable like @ershoaib's answer once you've accomplished your goal with the data.
2

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

Comments

1

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

Comments

0

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);

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.