c# - 在 C# 中解析 Json rest api 响应

标签 c# json rest api parsing

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

我有以下代码:

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内容如下:

{
  "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”?

我正在使用 C#。

最佳答案

1> 添加这个命名空间。 使用 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# - 在 C# 中解析 Json rest api 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18490599/

相关文章:

json - Postgres : Populate a JSON field with an array of values from a subquery

java - 集成 openid connect 进行 SPA 应用的最佳方式

javascript - 将参数附加到 api 请求?

javascript - 如何使用下划线js转换这些数据?

c# - 使用 MVC Core Identity 从 Web API 验证 MVC 网站用户

mysql - 如何将MySql多表结果集导出为JSON格式?

c# - 如何在 C# 中解析未知的日期时间格式

android - 使用从 AutoCompleteTextView 中选择的项目的相应数据设置隐藏字段的值

c# - 使用 ASP.NET MVC 分页在网页上显示日志文件信息

c# - 将数据发布到 asp.net Web API