CSV DATA导入可嵌套的json数据

时间:2015-04-01 07:33:26

标签: javascript jquery csv

晚上好。

我的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

感谢您的回答。但是下面显示的问题很少。 enter image description here

我不需要array[0]请帮助我。

1 个答案:

答案 0 :(得分:1)

首先我将csv数据解析为js对象:

var array = data.split("\n").map(function(row, index) { 
  var arr = row.split(",")
  return {
    id: index+1,
    level: +arr[0],
    label: arr[1],
    price: arr[2],
    children: []
  }
})

然后我添加一个0级,这样所有东西都有一个父级:

array.unshift({ level: 0, children: [] })
var parents = array.slice(0,1)

我循环数据,将level参数与前一个参数进行比较。我还将父母保持在一个堆栈中,底层为0级父级。

for(var i=1; i<array.length; i++) {
  var prev = array[i-1]
  var obj = array[i]
  var p = parents[parents.length-1]
  if(obj.level > prev.level) {
    parents.push(prev)
    p = prev
  } else if (obj.level < prev.level) {
    do {
      p = parents.pop()
    } while(obj.level < p.level)
    p = parents[parents.length-1]
  }
  p.children.push(obj)
}

最后我读出了结果

var result = parents[0].children
console.log(result)

工作示例:http://jsbin.com/zexiyigili/1/edit?js,console