简体   繁体   English

使用Newtonsoft在C#中迭代嵌套的JSON数组

[英]Iterating through a nested JSON Array in C# with Newtonsoft

I have a block of JSON as follows: 我有一块JSON如下:

[
  {
    "id": 1,
    "name": "Section1",
    "project_id": 100,
    "configs": [
      {
        "id": 1000,
        "name": "myItem1",
        "group_id": 1
      }
    ]
  },
  {
    "id": 2,
    "name": "Section2",
    "project_id": 100,
    "configs": [
      {
        "id": 1001,
        "name": "myItem2",
        "group_id": 2
      },
      {
        "id": 1002,
        "name": "myItem3",
        "group_id": 2
      },
      {
        "id": 1003,
        "name": "myItem4",
        "group_id": 2
      }
    ]
  },
  {
    "id": 3,
    "name": "Section3",
    "project_id": 100,
    "configs": [
      {
        "id": 1004,
        "name": "myItem5",
        "group_id": 5
      },
    ]
  }
]

I have pulled it into Memory as a JArray. 我把它作为JArray把它拉进了Memory。

I need to iterate through this such that I'm grabbing a list of ids and name from only the config sub-arrays. 我需要遍历这一点,以便我只从配置子数组中获取id列表。 Ideally, I'll end up with something like this: 理想情况下,我最终会得到这样的结论:

1000, myItem1
1001, myItem2
1002, myItem3
1003, myItem4
1004, myItem5

I'm having a hard time understanding what Newstonsoft calls a JObject vs a JArray, or how to access the various parts of each of those data structures. 我很难理解Newstonsoft称之为JObject与JArray的对象,或者如何访问每个数据结构的各个部分。 What I have right now is as follows: 我现在所拥有的如下:

foreach (JObject config in result["configs"])
{
    string id = (string)config["id"];
    string name = (string)config["name"];
    string gid = (string)config["group_id"];

    Console.WriteLine(name + " - " + id + " - " + gid);
}

This does not work, but I hope it illustrates what my end goal is. 这不起作用,但我希望它说明了我的最终目标。 I've been unable to piece together how to accomplish this goal. 我一直无法拼凑如何实现这一目标。

A JObject is an object (analogous to a class): JObject是一个对象(类似于一个类):

{
    "a": 1,
    "b": true
}

A JArray is a JSON array, and contains multiple JObject entities: JArray是一个JSON数组,包含多个JObject实体:

[
    {
        "a": 1,
        "b": true
    },
    {
        "a": 2,
        "b": true
    }
]

The root of a JSON document can be an object, or an array. JSON文档的根可以是对象或数组。 In your case, it's an array. 在你的情况下,它是一个数组。

The following code and fiddle reveals that your code is fine, provided that you deserialize the document as what it is - an array. 下面的代码和提示显示您的代码很好,只要您将文档反序列化为数组即可。

string json = "[{\"id\":1,\"name\":\"Section1\",\"project_id\":100,\"configs\":[{\"id\":1000,\"name\":\"myItem1\",\"group_id\":1}]},{\"id\":2,\"name\":\"Section2\",\"project_id\":100,\"configs\":[{\"id\":1001,\"name\":\"myItem2\",\"group_id\":2},{\"id\":1002,\"name\":\"myItem3\",\"group_id\":2},{\"id\":1003,\"name\":\"myItem4\",\"group_id\":2}]},{\"id\":3,\"name\":\"Section3\",\"project_id\":100,\"configs\":[{\"id\":1004,\"name\":\"myItem5\",\"group_id\":5},]}]";
JArray obj = Newtonsoft.Json.JsonConvert.DeserializeObject<JArray>(json);
foreach (var result in obj)
{
    foreach (JObject config in result["configs"])
    {
        string id = (string)config["id"];
        string name = (string)config["name"];
        string gid = (string)config["group_id"];

        Console.WriteLine(name + " - " + id + " - " + gid);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM