使用Newtonsoft遍历C#中的嵌套JSON数组 [英] Iterating through a nested JSON Array in C# with Newtonsoft

查看:298
本文介绍了使用Newtonsoft遍历C#中的嵌套JSON数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个如下的JSON块:

I have a block of JSON as follows:

[
  {
    "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拉入了内存.

I have pulled it into Memory as a JArray.

我需要对此进行迭代,以便仅从config子数组中获取ID和名称的列表.理想情况下,我将得到如下结果:

I need to iterate through this such that I'm grabbing a list of ids and name from only the config sub-arrays. Ideally, I'll end up with something like this:

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

我很难理解Newstonsoft所说的JObject与JArray,或者如何访问这些数据结构的各个部分.我现在所拥有的如下:

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. 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.

推荐答案

JObject是一个对象(类似于类):

A JObject is an object (analogous to a class):

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

JArray是一个JSON数组,包含多个JObject实体:

A JArray is a JSON array, and contains multiple JObject entities:

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

JSON文档的根可以是对象,也可以是数组.在您的情况下,它是一个数组.

The root of a JSON document can be an object, or an array. 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);
    }
}

这篇关于使用Newtonsoft遍历C#中的嵌套JSON数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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