在 C# 中解析 Json rest api 响应 [英] Parsing Json rest api response in C#

查看:46
本文介绍了在 C# 中解析 Json rest api 响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 C# 从 rest api json 响应中提取一个值.

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

我有以下代码:

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

在响应"消息中,我得到了如下的 json 内容:

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": "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
                }
              }
            }
          ]
        }
      }
    ]
  }
}

如何从返回的 json 响应结果消息中提取id"的以下嵌套值:521cfcd840926a0b3500449e"?

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

我使用的是 C#.

推荐答案

1> 添加此命名空间.使用 Newtonsoft.Json.Linq;

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

2> 使用此源代码.

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

这篇关于在 C# 中解析 Json rest api 响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆