2013-04-24 190 views
0

我想將MySQL數據庫數據轉換爲json格式。我的文件需要嵌套父母和孩子的格式。 (請參閱片段)這是因爲我試圖使用d3數據可視化(可摺疊樹佈局),它要求以某種方式格式化json數據。下面我附加了我所看到的json文件的片段。我有一些問題試圖編寫嵌套的SQL語句來做到這一點。我還附加了我的數據庫視圖,以瞭解我正在處理的內容。任何建議都會有很大幫助。謝謝! enter image description here試圖將MySQL數據轉換爲d3的嵌套json文件?

我試圖讓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":"[email protected]"}, 
         {"name":"dan", "email":"[email protected]"} 
         ] 
        }, 
        { 
        "name": "project2", 
        "description":"project 2 text goes here", 
           "children":[ 
         {"name":"steve", "email":"[email protected]"}, 
         {"name":"chris", "email":"[email protected]"} 
           ]      
        } 
        ] 
      }, 
      { 
      "name": "sub_category_name2", 
      "description":"sub category description text goes here..", 
      "children": [ 
       {"name": "project3", 
       "description":"project3 text goes here ", 
       "children":[ 
        {"name":"Alex", "email":"[email protected]"}       
       ]     
       } 
      ] 
      } 

    ] 
    }, 
    . 
    . //more categories with children of subcategories and sub_cat children of projects 
    . 
} 
+1

上使用的語言頁面請求動態生成的(是哪一個?紅寶石?蟒蛇?PHP的?),或者只是傾銷的現狀進行靜態文件由D3讀? – minikomi 2013-04-24 02:02:56

+0

對不起,沒有提及它。我使用PHP和動態的頁面請求。 – user1899872 2013-04-24 03:51:33

回答

0

我已經成功與本教程過去: http://www.d3noob.org/2013/02/using-mysql-database-as-source-of-data.html

總之你會做什麼是創建一個連接到MySql的php文件,執行查詢並將結果轉換爲json。然後,您將使用此文件來代替d3代碼中的數據源。

+0

好的謝謝。我會試一試。唯一的問題是我需要確保在必要時添加「name:」和「children:」標記,因爲我試圖創建的特定d3視覺需要該格式的數據。 – user1899872 2013-04-24 13:28:41

+0

@ user1899872你有沒有得到你的解決方案? – hawkeye 2015-08-08 08:46:05

+0

如何讓它像flare.json例子那樣做?我不認爲這回答了這個問題。 – johnny 2015-09-02 14:54:52