如何从数据库创建 JSON 树

  “我正在设置一个文件浏览器并将文件夹的名称保存在数据库中。任何数量的文件夹都可以保存在数据库中,我想从保存的数据中创建一个 json 树。


这是我的数据库表结构


folder_id    folder_name      parent_id

  1          parentFolder1    <NULL>

  2          parentFolder2    <NULL>

  3          subFolder1          1

  4          subFolder2          1

  5          subFolder3          3

  6          subFolder4          2

  7          subFolder5          5

parent_id 是指folder_id 的外键。


这是我尝试从上述数据库创建的 json 树结构,将来会添加更多父文件夹和子文件夹。如果可以从上面的表结构创建 json 树?谁能帮帮我吗?


[

    {

        "id": "1",

        "value": "parentFolder1",

        "data": [

            {

                "id": "3",

                "value": "subFolder1",

                "data": [

                    {

                        "id": "5",

                        "value": "subFolder3",

                        "data": [

                            {

                                "id": "7",

                                "value": "subFolder5",

                                "data": []  

                                ]

                            }

                        ]

                    }

            },

            {

                "id": "4",

                "value": "subFolder2",

                "data": []

            }

        ]

    },

    {

        "id": "2",

        "value": "parentFolder2",

        "data": [

            {

                "id": "6",

                "value": "subFolder4",

                "data": []

            }

        ]

    }

]


神不在的星期二
浏览 195回答 2
2回答

沧海一幻觉

$arr = array(&nbsp; array('id'=>100, 'parentid'=>0, 'name'=>'a'),&nbsp; array('id'=>101, 'parentid'=>100, 'name'=>'a'),&nbsp; array('id'=>102, 'parentid'=>101, 'name'=>'a'),&nbsp; array('id'=>103, 'parentid'=>101, 'name'=>'a'),);$new = array();foreach ($arr as $a){&nbsp; &nbsp; $new[$a['parentid']][] = $a;}$tree = createTree($new, array($arr[0]));print_r($tree);function createTree(&$list, $parent){&nbsp; &nbsp; $tree = array();&nbsp; &nbsp; foreach ($parent as $k=>$l){&nbsp; &nbsp; &nbsp; &nbsp; if(isset($list[$l['id']])){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $l['data'] = createTree($list, $list[$l['id']]);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; $tree[] = $l;&nbsp; &nbsp; }&nbsp;&nbsp; &nbsp; return $tree;}

有只小跳蛙

这是 C++ 代码示例,这不是一个完美的代码。您需要修改它以供您使用#if 0folder_id&nbsp; &nbsp; folder_name&nbsp; &nbsp; &nbsp; parent_id&nbsp; 1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; parentFolder1&nbsp; &nbsp; <NULL>&nbsp; 2&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; parentFolder2&nbsp; &nbsp; <NULL>&nbsp; 3&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; subFolder1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 1&nbsp; 4&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; subFolder2&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 1&nbsp; 5&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; subFolder3&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 3&nbsp; 6&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; subFolder4&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 2&nbsp; 7&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; subFolder5&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 5#endif#include<stdio.h>#include<iostream>#include<vector>#include<tuple>#include <bits/stdc++.h>#include<boost/property_tree/ptree.hpp>#include <boost/property_tree/json_parser.hpp>std::vector<std::tuple <int, std::string, int>> list;namespace pt = boost::property_tree;pt::ptree rootNode;int main () {&nbsp; &nbsp; list.push_back(std::make_tuple(1, "parentFolder1", 0));&nbsp; &nbsp; list.push_back(std::make_tuple(2, "parentFolder2", 0));&nbsp; &nbsp; list.push_back(std::make_tuple(3, "subFolder1", 1));&nbsp; &nbsp; list.push_back(std::make_tuple(4, "subFolder2", 1));&nbsp; &nbsp; list.push_back(std::make_tuple(5, "subFolder3", 3));&nbsp; &nbsp; list.push_back(std::make_tuple(6, "subFolder4", 2));&nbsp; &nbsp; list.push_back(std::make_tuple(7, "subFolder5", 5));&nbsp; &nbsp; int x, y;&nbsp; &nbsp; int k = 0;&nbsp; &nbsp; pt::ptree mainRootNode;&nbsp; &nbsp; for (int i=0; i < list.size(); i++) {&nbsp; &nbsp; &nbsp; &nbsp; if (std::get<2>(list[i])) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; continue;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; k = 0;&nbsp; &nbsp; &nbsp; &nbsp; pt::ptree subMainRootNode;&nbsp; &nbsp; &nbsp; &nbsp; subMainRootNode.add("id", std::get<0>(list[i]));&nbsp; &nbsp; &nbsp; &nbsp; subMainRootNode.add("value", std::get<1>(list[i]));&nbsp; &nbsp; &nbsp; &nbsp; pt::ptree Node;&nbsp; &nbsp; &nbsp; &nbsp; y = x = std::get<0>(list[i]);&nbsp; &nbsp; &nbsp; &nbsp; int saved_place = 0;&nbsp; &nbsp; &nbsp; &nbsp; for (int j = 0; j < list.size(); j++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (x == std::get<2>(list[j])) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; k++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; std::string str;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for(int m = 0;m < k; m++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; str.append("data.");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pt::ptree child;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; std::string id = str + "id";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; std::string value = str + "value";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; subMainRootNode.add(id, std::get<0>(list[j]));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; subMainRootNode.add(value, std::get<1>(list[j]));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; x = std::get<0>(list[j]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (!saved_place) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; saved_place = j;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (((j+1) == list.size()) && saved_place) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; j = saved_place ;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; k = 0;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; saved_place = 0;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; x = y;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; mainRootNode.add_child("data", subMainRootNode);&nbsp; &nbsp; &nbsp; &nbsp; pt::write_json(std::cout, subMainRootNode);&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP