JSON中的增量类别

本文关键字:JSON | 更新日期: 2023-09-27 18:27:32

我想从JSON文件中增量检索根类别、子类别和子类别的子类别。

如何"自动"获取所有类别、子类别和子类别的子类别。此时,我只得到根的根类别和子类别

每个类别和子类别可以有任意数量的类别。

这是我的CategoriesData.cs文件:

public class CategoriesData
{
   public List<Categories> categories { get; set; }
}

Categories.cs文件:

public class Categories
{
    public string id { get; set; }
    public string name { get; set; }
    public SubCategories subcategories { get; set; }
}

子类别.cs文件

public class SubCategories
{
    public List<ChildCat> children_categories { get; set; }
}

ChildCat.cs

public class ChildCat
{
    public string id { get; set; }
    public string name { get; set; }
}

下面是一个JSON文件示例:

根类别:(Root.json)

{
  "id": "root",
  "name": "categories",
  "categories": [
    {
      "id": "cat1",
      "name": "name1"
    },
    {
      "id": "cat2",
      "name": "name2"
    },
    {
      "id": "cat3",
      "name": "name3"
    },
    {
      "id": "cat4",
      "name": "name4"
    },
    {
      "id": "cat5",
      "name": "name5"
    }
  ]
}

如果我导航到任何一个类别,JSON看起来像:(在这个例子中,我导航到"cat1")(cat1.JSON)

{
  "id": "cat1",
  "name": "name1",
  "path_from_root": [
    {
      "id": "cat1",
      "name": "name1"
    }
  ],
  "children_categories": [
    {
      "id": "cat6",
      "name": "name6"
    },
    {
      "id": "cat7",
      "name": "name7"
    }
 ]
}

如果我导航到子类cat6,JSON看起来像:(文件:cat6.JSON)

{
  "id": "cat6",
  "name": "name6",
  "path_from_root": [
    {
      "id": "cat1",
      "name": "name1"
    },
    {
      "id": "cat6",
      "name": "name6"
    }
  ],
  "children_categories": [
    {
      "id": "cat8",
      "name": "name8"
    },
    {
      "id": "cat9",
      "name": "name9"
    }
  ]
}

如果我导航到cat9,并且没有更多的子类别,那么文件看起来像:(childen_categories数组为空)(文件:cat9.json)

{
  "id": "cat9",
  "name": "name9",
  "path_from_root": [
    {
      "id": "cat1",
      "name": "name1"
    },
    {
      "id": "cat6",
      "name": "name6"
    },
    {
      "id": "cat9",
      "name": "name9"
    }
  ],
  "children_categories": [
  ],
}

我实现这个来获得根类别和子类别:

JavaScriptSerializer serializer = new JavaScriptSerializer();
CategoriesData categoriesData;
categoriesData = serializer.Deserialize<CategoriesData>(root.json);
foreach (var item in categoriesData.categories)
{
                    string json = string.Format("{0}.json", item.id);
                    item.subcategories = serializer.Deserialize<SubCategories>(json);
}

JSON中的增量类别

好吧,你想为此写一个递归函数,比如:

private static readonly JavaScriptSerializer Serializer =
    new JavaScriptSerializer();
private static Category FillCategory(Category category)
{
    var path = string.Format("{0}.json", category.Id);
    var text = File.ReadAllText(path);
    var result = Serializer.Deserialize<Category>(text);
    for (int i = 0; i < result.Children_Categories.Count; i++)
        result.Children_Categories[i] =
            FillCategory(result.Children_Categories[i]);
    return result;
}

然后像这样使用:

var text = File.ReadAllText("root.json");
var categoriesData = Serializer.Deserialize<CategoriesData>(text);
var categories = categoriesData.Categories.Select(FillCategory).ToList();

这假设您修改了模型以实际匹配JSON,即没有SubCategories类。