0
我有一個CSV數據顯示如下。
level,label,price
1,menu1,3000
2,menu1_1,5000
2,menu1_2,6000
2,menu1_3,7000
1,menu2,8000
2,menu2_1,5000
3,menu2_1_1,5000
3,menu2_1_2,7000
2,menu2_2,6000
2,menu2_3,7000
1,menu3,9000
1,menu4,10000
我想將此數據轉換爲json數據,如下所示。有必要遞歸表單應該寫我看到的任何JavaScript代碼?
[
{
"id": 1,
"label": "menu1",
"price": "3000",
"children": [
{
"id": 2,
"label": "menu1_1",
"price": "5000",
"children": []
},
{
"id": 3,
"label": "menu1_2",
"price": "6000",
"children": []
},
{
"id": 4,
"label": "menu1_1",
"price": "7000",
"children": []
}
]
},
{
"id": 5,
"label": "menu2",
"price": "8000",
"children": [
{
"id": 6,
"label": "menu2_1",
"price": "5000",
"children": [
{
"id": 7,
"label": "menu2_1_1",
"price": "5000",
"children": []
},
{
"id": 8,
"label": "menu2_1_2",
"price": "7000",
"children": []
}
]
},
{
"id": 9,
"label": "menu2_2",
"price": "6000",
"children": []
},
{
"id": 10,
"label": "menu2_3",
"price": "7000",
"children": []
}
]
},
{
"id": 11,
"label": "menu3",
"price": "9000",
"mnu_img": "",
"index": 46,
"children": []
},
{
"id": 12,
"label": "menu4",
"price": "10000",
"mnu_img": "",
"index": 50,
"children": []
}
]
但是,我有一個轉換導入CSV源。 注意網址:http://techslides.com/convert-csv-to-json-in-javascript
感謝您的回答。但是下面顯示了一些小問題。
我不需要array[0]
請幫幫我。
哦,這就是我的回答@OLLiM問題。完善。 'var array = data.split(/ \ r \ n /)。圖(函數(行,索引){' 這裏是代碼的方式,第一行上的主題行應該去零線我喜歡,如果你想忽略和放?首先,由於主題行csv線。 – taebu 2015-04-02 01:16:21