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

在"response"消息中,我得到了一个json内容,如下所示:

{
  "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": "roy@tomax.co.il",
          "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": "roy@tomax.co.il",
              "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?

我用的是C#.

推荐答案

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

Json相关问答推荐

jq不会为空输入返回非零

输入请求中不存在null的条件抖动

我如何知道TJSONNumber是double还是double?

Pandas 对REST API的自定义响应

交换键和数组值,将旧键转换为新数组值,使用 jq

当列为空时从 SQL 获取 JSON

jq如何合并两个json对象

如何按键过滤

使用 jq Select 键:值并输出为数组

将 YAML 文件转换为 Python JSON 对象

如何在 jQuery 中循环遍历 JSON 数组?

Jackson 的@JsonView、@JsonFilter 和 Spring

如何使用 Swift 从 NSURLSession 获取 cookie?

在 JSON 字符串中react i18n 换行符

在 Http Header 中使用 Json 字符串

使用 gson 反序列化对象的特定 JSON 字段

JSON.NET JsonConvert 与 .NET JavaScriptSerializer

如何从 MVC 控制器返回 Json 对象以查看

在 iPhone 上解析 JSON 日期

将 Pandas 数据框转换为嵌套 JSON