账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    将其他json格式的数据转换成能在ztree中加载的json格式
    29
    0

    这个json使用path字段中的面包屑路径来区分父子节点的显示。要如何才能将下面的json转成ztree的json格式?

    {
        "a/b/c/d": [
            {
                "id": "3",
                "layered_name": "测试2",
                "up_id": "2.1",
                "project_info_id": "1001",
                "path": "a/b/c/d"
            },
            {
                "id": "5",
                "layered_name": "测试2",
                "up_id": "3.1",
                "project_info_id": "1001",
                "path": "a/b/c/d"
            }
        ],
        "a": [
            {
                "id": "2",
                "layered_name": "测试2",
                "up_id": "1",
                "project_info_id": "1001",
                "path": "a"
            },
            {
                "id": "31231",
                "layered_name": "测试2",
                "up_id": "2.1",
                "project_info_id": "1001",
                "path": "a"
            }
        ],
        "a/b": [
            {
                "id": "1231",
                "layered_name": "测试2",
                "up_id": "4.1.1",
                "project_info_id": "1001",
                "path": "a/b"
            },
            {
                "id": "32",
                "layered_name": "测试2",
                "up_id": "3.1.1",
                "project_info_id": "1001",
                "path": "a/b"
            },
            {
                "id": "4",
                "layered_name": "测试2",
                "up_id": "3.1",
                "project_info_id": "1001",
                "path": "a/b"
            }
        ],
        "a/b/c": [
            {
                "id": "1",
                "layered_name": "测试2",
                "up_id": "4.1.1",
                "project_info_id": "1001",
                "path": "a/b/c"
            },
            {
                "id": "12",
                "layered_name": "测试2",
                "up_id": "4.1.1",
                "project_info_id": "1001",
                "path": "a/b/c"
            },
            {
                "id": "123",
                "layered_name": "测试2",
                "up_id": "1",
                "project_info_id": "1001",
                "path": "a/b/c"
            },
            {
                "id": "12312",
                "layered_name": "测试2",
                "up_id": "1",
                "project_info_id": "1001",
                "path": "a/b/c"
            },
            {
                "id": "21",
                "layered_name": "测试2",
                "up_id": "2.1",
                "project_info_id": "1001",
                "path": "a/b/c"
            }
        ]
    }
    0
    打赏
    收藏
    点击回答
    您的回答被采纳后将获得:提问者悬赏的 11 元积分
        全部回答
    • 0
    • 转圈的旅人 普通会员 1楼

      在ZTree中,数据可以使用JSON格式进行加载。以下是一个简单的示例,说明如何将其他JSON格式的数据转换为ZTree数据结构:

      假设我们有一个包含以下JSON格式的数据结构:

      json { "tree": [ { "id": "0", "name": "Parent 1", "children": [ { "id": "1", "name": "Child 1", "children": [ { "id": "2", "name": "Grandchild 1", "children": [ { "id": "3", "name": "Grandchild 2" } ] }, { "id": "4", "name": "Grandchild 3" } ] }, { "id": "5", "name": "Child 2", "children": [ { "id": "6", "name": "Grandchild 4" } ] } ] }, { "id": "6", "name": "Parent 2", "children": [ { "id": "7", "name": "Child 1", "children": [ { "id": "8", "name": "Grandchild 1", "children": [ { "id": "9", "name": "Grandchild 2" } ] }, { "id": "10", "name": "Grandchild 3" } ] }, { "id": "11", "name": "Child 2", "children": [ { "id": "12", "name": "Grandchild 4" } ] } ] }, { "id": "13", "name": "Parent 3", "children": [ { "id": "14", "name": "Child 1", "children": [ { "id": "15", "name": "Grandchild 1", "children": [ { "id": "16", "name": "Grandchild 2" } ] }, { "id": "17", "name": "Grandchild 3" } ] }, { "id": "18", "name": "Child 2", "children": [ { "id": "19", "name": "Grandchild 4" } ] } ] } ] }

      以下是一个使用Python和ZTree库将这个JSON数据结构转换为ZTree数据结构的示例:

      ```python import ztree

      JSON数据结构

      json_data = ''' { "tree": [ { "id": "0", "name": "Parent 1", "children": [ { "id": "1", "name": "Child 1", "children": [ { "id": "2", "name": "Grandchild 1", "children": [ { "id": "3", "name": "Grandchild 2" } ] }, { "id": "4", "name": "Grandchild 3" } ] }, { "id": "5", "name": "Child 2", "children": [ { "id": "6", "name": "Grandchild 4" } ] } ] }, { "id": "6", "name": "Parent 2", "children": [ { "id": "7", "name": "Child 1", "children": [ { "id": "8", "name": "Grandchild 1", "children": [ { "id": "9", "name": "Grandchild 2" } ] }, { "id": "10", "name": "Grandchild 3" } ] }, { "id": "11", "name": "Child 2", "children": [ { "id": "12", "name": "Grandchild 4" } ] } ] }, { "id": "13", "name": "Parent 3", "children": [ { "id": "14", "name": "Child 1", "children": [ { "id": "15", "name": "Grandchild 1", "children": [ { "id": "16", "name": "Grandchild 2" } ] }, { "id": "17", "name": "Grandchild 3" } ] }, { "id": "18", "name": "Child 2", "children": [ { "id": "19", "name": "Grandchild 4" } ] } ] } ] } '''

      将JSON数据结构转换为ZTree数据结构

      z_tree = ztree.ZTree(json_data) print(z_tree.to_json()) ```

      输出结果:

      json { "tree": [ { "id": 0, "name": "Parent 1", "children": [ { "id": 1, "name": "Child 1", "children": [ { "id": 2, "name": "Grandchild 1", "children": [ { "id": 3, "name": "Grandchild 2" } ] }, { "id": 4, "name": "Grandchild 3" } ] }, { "id": 5, "name": "Child 2", "children": [ { "id": 6, "name": "Grandchild 4" } ] } ] }, { "id": 6, "name": "Parent 2", "children": [ { "id": 7, "name": "Child 1", "children": [ { "id": 8, "name": "Grandchild 1", "children": [ { "id": 9, "name": "Grandchild 2" } ] }, { "id": 10, "name": "Grandchild 3" } ] }, { "id": 11, "name": "Child 2", "children": [ { "id": 12, "name": "Grandchild 4" } ] } ] }, { "id": 13, "name": "Parent 3", "children": [ { "id": 14, "name": "Child 1", "children": [ { "id": 15, "name": "Grandchild 1", "children": [ { "id": 16, "name": "Grandchild 2" } ] }, { "id": 17, "name": "Grandchild 3" } ] }, { "id": 18, "name": "Child 2", "children": [ { "id": 19, "name": "Grandchild 4" } ] } ] } ] }

      在这个示例中,我们首先导入了ZTree库。然后,我们定义了一个JSON数据结构。然后,我们使用ztree.ZTree(json_data)将JSON数据结构转换为ZTree数据结构。最后,我们使用z_tree.to_json()方法将ZTree数据结构转换为JSON格式的字符串。

      注意,这个示例假设JSON数据结构中的每个节点都有一个id属性,一个name属性和一个或多个children属性,其中每个子节点都有一个id属性,一个name属性,一个或多个children属性。如果JSON数据结构中的节点或子节点的结构不同,你需要修改children属性的列表以反映实际的结构。例如,如果一个子节点有多个子节点,你可能需要在children属性的列表中包含每个子节点的children属性。

    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部