我正在尝试将 MySQL 数据库数据转换为 json 格式。我的文件需要采用嵌套父母和孩子的格式。(见代码片段)这是因为我正在尝试使用 d3 数据可视化(可折叠树布局),它需要以某种方式格式化 json 数据。下面我附上了我的 json 文件的片段。我在尝试编写嵌套的 sql 语句来执行此操作时遇到一些问题。我还附上了我的数据库的视觉效果,以查看我正在处理的内容。任何建议都会有很大帮助。谢谢!
我想要得到的 json 格式是这样的:
{
"name": "Projects",
"children": [
{
"name":"category_name#1", "description":"category",
"children": [
{
"name":"sub_category_name1",
"description":"category description text here",
"children":[
{"name": "project1",
"description":"project 1 text goes here",
"children":[
{"name":"mike", "email":"xxx@mail.com"},
{"name":"dan", "email":"xxx@gmail.com"}
]
},
{
"name": "project2",
"description":"project 2 text goes here",
"children":[
{"name":"steve", "email":"xxx@mail.com"},
{"name":"chris", "email":"xxx@gmail.com"}
]
}
]
},
{
"name": "sub_category_name2",
"description":"sub category description text goes here..",
"children": [
{"name": "project3",
"description":"project3 text goes here ",
"children":[
{"name":"Alex", "email":"xxx@gmail.com"}
]
}
]
}
]
},
.
. //more categories with children of subcategories and sub_cat children of projects
.
}