试图将MySQL数据导入到d3的嵌套json文件中?

时间:2013-04-24 01:32:42

标签: mysql json treeview d3.js nested-queries

我正在尝试将MySQL数据库数据转换为json格式。我的文件需要采用嵌套父项和子项的格式。 (参见片段)这是因为我试图使用d3数据可视化(可折叠树布局),要求以某种方式格式化json数据。下面我附上了一个json文件的样子片段。我在尝试编写嵌套的sql语句时遇到了一些问题。我还附上了我的数据库视觉,看看我在处理什么。任何建议都会有很大帮助。谢谢! enter image description here

我想要的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
    .
}

1 个答案:

答案 0 :(得分:1)

我过去在本教程中取得了成功: http://www.d3noob.org/2013/02/using-mysql-database-as-source-of-data.html

简而言之,您要做的是创建一个连接到MySql的php文件,执行查询并将结果转换为json。然后,您将使用此文件代替d3代码中的数据源。