这是一开始的形式(部分)

[
  {
    "id": "A",
    "name": "农、林、牧、渔业",
    "pid": null,
    "level": "0",
    "desc": "本门类包括 01~05 大类"
  },
  {
    "id": "A01",
    "name": "农业",
    "pid": "A",
    "level": "1",
    "desc": "指对各种农作物的种植"
  },
  {
    "id": "A011",
    "name": "谷物种植",
    "pid": "A01",
    "level": "2",
    "desc": "指以收获籽实为主的农作物的种植,包括稻 谷、小麦、玉米等农作物的种植和作为饲料和工业原料的谷物的种植"
  },
  {
    "id": "A0111",
    "name": "稻谷种植",
    "pid": "A011",
    "level": "3",
    "desc": null
  },
  {
    "id": "A0112",
    "name": "小麦种植",
    "pid": "A011",
    "level": "3",
    "desc": null
  },
  {
    "id": "A0113",
    "name": "玉米种植",
    "pid": "A011",
    "level": "3",
    "desc": null
  },
  {
    "id": "A0119",
    "name": "其他谷物种植",
    "pid": "A011",
    "level": "3",
    "desc": null
  },
  {
    "id": "A012",
    "name": "豆类、油料和薯类种植",
    "pid": "A01",
    "level": "2",
    "desc": null
  }
]

然后将他们转换成这种形式

[
    {
        "id": "A",
        "name": "农、林、牧、渔业",
        "pid": null,
        "level": "0",
        "desc": "本门类包括 01~05 大类",
        "children": [
            {
                "id": "A01",
                "name": "农业",
                "pid": "A",
                "level": "1",
                "desc": "指对各种农作物的种植",
                "children": [
                    {
                        "id": "A011",
                        "name": "谷物种植",
                        "pid": "A01",
                        "level": "2",
                        "desc": "指以收获籽实为主的农作物的种植,包括稻 谷、小麦、玉米等农作物的种植和作为饲料和工业原料的谷物的种植",
                        "children": [
                            {
                                "id": "A0111",
                                "name": "稻谷种植",
                                "pid": "A011",
                                "level": "3",
                                "desc": null
                            },
                            {
                                "id": "A0112",
                                "name": "小麦种植",
                                "pid": "A011",
                                "level": "3",
                                "desc": null
                            }

下面是python代码

# 划分同一个数据下面的各种类别
import json

# 读取文件并解析 JSON 数据
def read_json_file(file_path):
    with open(file_path, 'r', encoding='utf-8') as file:
        data = json.load(file)
    return data

# 写入到 JSON 文件
def write_json_file(data, file_path):
    with open(file_path, 'w', encoding='utf-8') as file:
        json.dump(data, file, ensure_ascii=False, indent=4)

def build_hierarchy(categories):
    # 创建一个空的字典列表,用于存储最终结果
    result = []

    # 创建字典,用于将分类按照 id 分组
    category_map = {}
    for category in categories:
        category_map[category['id']] = category

    # 遍历分类列表,构建分类树
    for category in categories:
        # 如果当前分类的 pid 为空,则将其视为一级分类
        if category['pid'] is None:
            result.append(category)
        # 否则,将当前分类添加到其父分类的 children 列表中
        else:
            parent_id = category['pid']
            parent_category = category_map.get(parent_id)
            if parent_category is not None:
                if 'children' not in parent_category:
                    parent_category['children'] = []
                parent_category['children'].append(category)

    return result




# 读取数据
categories = read_json_file('D:\ALL_Proj\PyCharm_Proj\pythonProjectDemo01\无标题.json')
# 构建分类树
result = build_hierarchy(categories)
# 写入数据
write_json_file(result, 'D:\ALL_Proj\PyCharm_Proj\pythonProjectDemo01\Aresult.json')

print("文件写入成功")
最后修改:2024 年 11 月 04 日
如果觉得我的文章对你有用,请随意赞赏