Parsing Json rest api response in C#

137,391

Solution 1

1> Add this namspace. using Newtonsoft.Json.Linq;

2> use this source code.

JObject joResponse = JObject.Parse(response);                   
JObject ojObject = (JObject)joResponse["response"];
JArray array= (JArray)ojObject ["chats"];
int id = Convert.ToInt32(array[0].toString());

Solution 2

  1. Create classes that match your data,
  2. then use JSON.NET to convert the JSON data to regular C# objects.

Step 1: a great tool - http://json2csharp.com/ - the results generated by it are below

Step 2: JToken.Parse(...).ToObject<RootObject>().

public class Meta
{
    public int code { get; set; }
    public string status { get; set; }
    public string method_name { get; set; }
}

public class Photos
{
    public int total_count { get; set; }
}

public class Storage
{
    public int used { get; set; }
}

public class Stats
{
    public Photos photos { get; set; }
    public Storage storage { get; set; }
}

public class From
{
    public string id { get; set; }
    public string first_name { get; set; }
    public string created_at { get; set; }
    public string updated_at { get; set; }
    public List<object> external_accounts { get; set; }
    public string email { get; set; }
    public string confirmed_at { get; set; }
    public string username { get; set; }
    public string admin { get; set; }
    public Stats stats { get; set; }
}

public class ParticipateUser
{
    public string id { get; set; }
    public string first_name { get; set; }
    public string created_at { get; set; }
    public string updated_at { get; set; }
    public List<object> external_accounts { get; set; }
    public string email { get; set; }
    public string confirmed_at { get; set; }
    public string username { get; set; }
    public string admin { get; set; }
    public Stats stats { get; set; }
}

public class ChatGroup
{
    public string id { get; set; }
    public string created_at { get; set; }
    public string updated_at { get; set; }
    public string message { get; set; }
    public List<ParticipateUser> participate_users { get; set; }
}

public class Chat
{
    public string id { get; set; }
    public string created_at { get; set; }
    public string updated_at { get; set; }
    public string message { get; set; }
    public From from { get; set; }
    public ChatGroup chat_group { get; set; }
}

public class Response
{
    public List<Chat> chats { get; set; }
}

public class RootObject
{
    public Meta meta { get; set; }
    public Response response { get; set; }
}

Solution 3

Create a C# class that maps to your Json and use Newsoft JsonConvert to Deserialise it.

For example:

public Class MyResponse
{
    public Meta Meta { get; set; }
    public Response Response { get; set; }
}
Share:
137,391
Chehmer
Author by

Chehmer

Updated on July 18, 2022

Comments

  • Chehmer
    Chehmer almost 2 years

    I am trying to pull a value from a rest api json response using C#.

    I have the following code:

    client.BaseUrl = "https://api.cloud.appcelerator.com";
    request.Resource = "/v1/chats/create.json?key=" + cac.AppCode.ToString();
    request.Method = Method.POST;
    request.AddUrlSegment("appkey", "key");
    var response = client.Execute(request);
    

    In the "response" message I got a json content as follows:

    {
      "meta": {
        "code": 200,
        "status": "ok",
        "method_name": "createChatMessage"
      },
      "response": {
        "chats": [
          {
            "id": "521cfcd840926a0b3500449e",
            "created_at": "2013-08-27T19:24:08+0000",
            "updated_at": "2013-08-27T19:24:08+0000",
            "message": " join to the chat group, welcome …",
            "from": {
              "id": "520f41e125e74b0b2400130a",
              "first_name": "Administrator",
              "created_at": "2013-08-17T09:26:57+0000",
              "updated_at": "2013-08-27T19:23:10+0000",
              "external_accounts": [
    
              ],
              "email": "[email protected]",
              "confirmed_at": "2013-08-17T09:26:57+0000",
              "username": "admin",
              "admin": "true",
              "stats": {
                "photos": {
                  "total_count": 0
                },
                "storage": {
                  "used": 0
                }
              }
            },
            "chat_group": {
              "id": "521cfcd840926a0b3500449d",
              "created_at": "2013-08-27T19:24:08+0000",
              "updated_at": "2013-08-27T19:24:08+0000",
              "message": " join to the chat group, welcome …",
              "participate_users": [
                {
                  "id": "520f41e125e74b0b2400130a",
                  "first_name": "Administrator",
                  "created_at": "2013-08-17T09:26:57+0000",
                  "updated_at": "2013-08-27T19:23:10+0000",
                  "external_accounts": [
    
                  ],
                  "email": "[email protected]",
                  "confirmed_at": "2013-08-17T09:26:57+0000",
                  "username": "admin",
                  "admin": "true",
                  "stats": {
                    "photos": {
                      "total_count": 0
                    },
                    "storage": {
                      "used": 0
                    }
                  }
                }
              ]
            }
          }
        ]
      }
    }
    

    How do I pull the following nested value of "id": "521cfcd840926a0b3500449e" from the returned json response result message?

    I am using C#.

  • Hajjat
    Hajjat over 6 years
    Dude, the json2csharp website is amazing! Thanks for sharing!
  • Jamshaid K.
    Jamshaid K. about 6 years
    I am doing the same thing but it seems it violates the C# Coding Convention like properties without a capital letter starting. Can you recommend how to make this example even better?
  • DDuffy
    DDuffy over 3 years
    2 days... that's how long i have been looking for an answer that works. And here it is. Thank you
  • Paulo Guimarães
    Paulo Guimarães about 3 years
    Finally!! This is the only answer which really works!
  • Jaideep Dhumal
    Jaideep Dhumal over 2 years
    My json was similar to this, so had to modify this code a bit, but yeah, it really helped. Thanks a lot!