Iterating through a nested JSON Array in C# with Newtonsoft

岁酱吖の 提交于 2020-01-01 10:05:09

问题


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
      },
    ]
  }
]

I have pulled it into Memory as a JArray.

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

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.


回答1:


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

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

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

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

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


来源:https://stackoverflow.com/questions/47425419/iterating-through-a-nested-json-array-in-c-sharp-with-newtonsoft

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!