【问题标题】:C# Extract JSON from nested arrayC# 从嵌套数组中提取 JSON
【发布时间】:2017-05-26 20:47:41
【问题描述】:

我正在尝试使用 C# 和 JSON.NET 遍历嵌套的 JSON 数组。 JSON 代表在线网上商店的类别 - 下面是一个示例。我的目标是创建一个包含所有类别名称的列表。

{
  "id": 2,
  "parent_id": 1,
  "name": "Main Category List",
  "is_active": true,
  "position": 1,
  "level": 1,
  "product_count": 0,
  "children_data": [
    {
      "id": 9,
      "parent_id": 2,
      "name": "Mens Clothing",
      "is_active": true,
      "position": 6,
      "level": 2,
      "product_count": 0,
      "children_data": []
    },
    {
      "id": 8,
      "parent_id": 2,
      "name": "Womens Clothing",
      "is_active": true,
      "position": 7,
      "level": 2,
      "product_count": 0,
      "children_data": [
        {
          "id": 223,
          "parent_id": 8,
          "name": "Outdoor Clothing",
          "is_active": true,
          "position": 1,
          "level": 3,
          "product_count": 0,
          "children_data": []
        },
        {
          "id": 224,
          "parent_id": 8,
          "name": "Hiking Clothing",
          "is_active": true,
          "position": 2,
          "level": 3,
          "product_count": 0,
          "children_data": []
        },
        {
          "id": 596,
          "parent_id": 8,
          "name": "Dresses",
          "is_active": true,
          "position": 3,
          "level": 3,
          "product_count": 0,
          "children_data": [
            {
              "id": 694,
              "parent_id": 596,
              "name": "Summer Dresses",
              "is_active": true,
              "position": 13,
              "level": 4,
              "product_count": 0,
              "children_data": [
                {
                  "id": 720,
                  "parent_id": 694,
                  "name": "Accessories",
                  "is_active": true,
                  "position": 1,
                  "level": 5,
                  "product_count": 0,
                  "children_data": [ ]
                }
              ]
            }
          ]
        }
      ]
    },
    {
      "id": 10,
      "parent_id": 2,
      "name": "Sale & Clearance",
      "is_active": true,
      "position": 8,
      "level": 2,
      "product_count": 0,
      "children_data": []
    }
  ]
}

可能有不同级别的类别,我需要解析每个类别。我想获取每个类别并创建地图。例如(主分类列表 --> 女装 --> 户外服装)。我在想我可以检查儿童数据的深度,但我不知道如何不断深入地检查下一个 Json 对象。

JObject responseObject = JObject.Parse(response.Content);


foreach (JObject category in getCatResponseObj.SelectToken("children_data"))
{
    while loop checking depth of children_data



}

【问题讨论】:

    标签: c# json json.net


    【解决方案1】:

    如果是我,我会创建我能想到的最完整的 JSON 文件(包括所有可能的条目),然后使用 json2csharp 或等效工具来创建 c# 类,然后反序列化 Json 并在本地使用它。您可能需要稍微调整一下结果 - 但我认为您可以到达那里。如果这不起作用,我会使用 Newtonsofts JSON LINQ Features (documentation)。 JToken 为您提供父/子组合,允许您上下遍历树。文档中的示例非常好,所以不需要我用重复的信息填充页面。

    (根据您的示例生成)

    public class ChildrenData
    {
        public int id { get; set; }
        public int parent_id { get; set; }
        public string name { get; set; }
        public bool is_active { get; set; }
        public int position { get; set; }
        public int level { get; set; }
        public int product_count { get; set; }
        public List<object> children_data { get; set; }
    }
    
    public class RootObject
    {
        public int id { get; set; }
        public int parent_id { get; set; }
        public string name { get; set; }
        public bool is_active { get; set; }
        public int position { get; set; }
        public int level { get; set; }
        public int product_count { get; set; }
        public List<ChildrenData> children_data { get; set; }
    }
    

    【讨论】:

      【解决方案2】:

      这似乎是一个递归定义的结构。您应该创建一个函数来提取每个孩子的值,以便递归地重用它。

      IEnumerable<string> GetCategoryNames(JObject data)
      {
          yield return (string)data["name"];
          foreach (var name in data["children_data"].Cast<JObject>().SelectMany(GetCategoryNames))
              yield return name;
      }
      

      然后在根对象上调用它以将您的姓名放入列表或其他任何内容中。

      var obj = JObject.Parse(response.Content);
      var names = GetCategoryNames(obj).ToList();
      

      否则,如果您想不加选择地获取对象树中的所有名称,请记住 SelectToken()/SelectTokens() 也接受 json 路径查询。因此,要获取所有后代的所有名称,您只需这样做:

      let names = obj.SelectTokens("..name").ToList();
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-09-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多