将JSON反序列化为一个对象数组,而不是一个对象列表 [英] Deserialize JSON to an array of objects, instead of to one object list

查看:177
本文介绍了将JSON反序列化为一个对象数组,而不是一个对象列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为可能令人困惑的标题致歉.我是JSON的新手,并且正在使用与API交互的软件来开发Web应用程序.我都控制了.

Apologies for the probably confusing title. I'm new to JSON and I'm working on a Web Application with a piece of software which interfaces with the API. I have control of both.

我的应用程序需要枚举客户端"及其项目"的列表.当前,它返回以下内容:

My applications needs to enumerate a list of "Clients" and their "Projects". Currently it returns the following:

{
  "clients": [
    {
      "client_id": "1",
      "client_name": "Client1",
      "projects": [
        {
          "client_project_id": "1",
          "client_project_title": "WidgetsA",
          "client_project_client": "1",
          "client_project_status": "1"
        },
        {
          "client_project_id": "11",
          "client_project_title": "WidgetsB",
          "client_project_client": "1",
          "client_project_status": "1"
        }
      ]
    },
    {
      "client_id": "11",
      "client_name": "Client11",
      "projects": [
        {
          "client_project_id": "31",
          "client_project_title": "Install",
          "client_project_client": "11",
          "client_project_status": "1"
        }
      ]
    },
    {
      "client_id": "21",
      "client_name": "Client21",
      "projects": [
        {
          "client_project_id": "61",
          "client_project_title": "Marketing",
          "client_project_client": "21",
          "client_project_status": "1"
        }
      ]
    },
    {
      "client_id": "31",
      "client_name": "Client31",
      "projects": [
        {
          "client_project_id": "71",
          "client_project_title": "Fire Everyone",
          "client_project_client": "31",
          "client_project_status": "1"
        },
        {
          "client_project_id": "81",
          "client_project_title": "Buy A Company",
          "client_project_client": "31",
          "client_project_status": "1"
        }
      ]
    }
  ]
}

我可以使用以下JSON.NET代码将其轻松反序列化为对象:

I can deserialize this easily enough to an object using the following JSON.NET code:

MyObject result = JsonConvert.DeserializeObject<MyObject>(response);

但是,这仅在我的对象如下所示时有效:

However, this only works if my objects look like the following:

  [Serializable, JsonObject]
    internal class MyObject
    {
        [JsonProperty("clients")]
        internal List<ClientObject> ClientList = new List<ClientList>();
    }


 internal class ClientObject
    {

        [JsonProperty("client_id")]internal string ClientID { get; set; }

        [JsonProperty("client_name")] internal string ClientName { get; set; }

        [JsonProperty("projects")] internal List<ProjectObject> ProjectList = new List<ProjectObject>();
    }

(注意:为了保护隐私,我更改了许多属性和对象的名称,对于添加的任何错误,我们深表歉意)

(NB: I've changed the names of a lot of properties and objects for privacy, so apologies for any mistakes added)

我真正想做的是使用以下代码:

What I really want to be able to do is use the following code:

List<ClientObject> result = JsonConvert.DeserializeObject<List<ClientObject>>(response);

但是无论我如何格式化JSON响应,JSON.NET都会引发一个错误,指出JSON不是数组.谁能告诉我我要去哪里错了,或者我误解了什么?

But no matter how I format the JSON response, JSON.NET throws an error stating that the JSON is not an array. Can anyone advise where I'm going wrong, or what I'm misunderstanding?

推荐答案

您的JSON是一个对象,其中包含一个数组字段.您不能直接将其转换为数组.

Your JSON is an object, that contains a field that is an array. You cannot directly convert it to array.

我可以使用此JSON:

You can use this JSON I suppose:

[
    {
      "client_id": "1",      
    },
    {
      "client_id": "2",      
    }
]

但是,认真的说,拥有一个内部包含数组的适当的响应对象并没有什么坏处.我希望我们这样做.

But seriously, it does not hurt to have a proper response object that contains an array inside. I would rather us it.

这篇关于将JSON反序列化为一个对象数组,而不是一个对象列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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