【问题标题】:Parsing Json rest api response in C# [duplicate]在C#中解析Json rest api响应[重复]
【发布时间】:2013-08-28 14:20:53
【问题描述】:

我正在尝试使用 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#。

【问题讨论】:

标签: c# json rest api parsing


【解决方案1】:

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

【讨论】:

  • 2 天......这就是我寻找有效答案的时间。就在这里。谢谢
  • 终于!!这是唯一真正有效的答案!
  • 我的 json 和这个类似,所以不得不稍微修改一下这段代码,但是是的,它确实有帮助。非常感谢!
【解决方案2】:
  1. 创建与您的数据匹配的类,
  2. 然后使用 JSON.NET 将 JSON 数据转换为常规 C# 对象。

第 1 步:一个很棒的工具 - http://json2csharp.com/ - 它生成的结果如下

第 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; }
}

【讨论】:

  • 老兄,json2csharp 网站太棒了!感谢分享!
  • 我也在做同样的事情,但它似乎违反了C# 编码约定,如properties without a capital letter starting. 你能推荐如何让这个例子变得更好吗?
【解决方案3】:

创建一个映射到您的 Json 的 C# 类并使用 Newsoft JsonConvert 对其进行反序列化。

例如:

public Class MyResponse
{
    public Meta Meta { get; set; }
    public Response Response { get; set; }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-05-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-23
    • 2014-07-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多