JSON脚本格式化

时间:2016-03-02 05:34:00

标签: javascript d3.js

我正在研究D3 JS脚本。我正在编写一个函数来解析特定格式的数据。

预期产出:

  {
        "nodes": [
        { "name": "Agricultural 'waste'" },
        { "name": "Bio-conversion" },
        { "name": "Liquid" },
        { "name": "Losses" },
        { "name": "Solid" } 
],
"links": [
        { "source": 0, "target": 1, "value": 124.729 },
        { "source": 1, "target": 2, "value": 0.597 },
        { "source": 1, "target": 3, "value": 26.862 },
        { "source": 1, "target": 4, "value": 280.322 },
        { "source": 1, "target": 5, "value": 81.144 }
]
}

功能:

function parseData(Data) {
    var parsedRows = {nodes:[], links:[]};
    for (var i = 0; i < Data.data.length; i++) {
        parsedRows.nodes.push({"name":Data.data[i].items[0]});
        var link = 
    }
    return parsedRows;
}

,其中 数据的格式为:

data: Array[5]
0: Object
hints: Object
items: Array[2]
0: "Agricultural waste"
1: "[{ "target": 1, "value": 124.729 }]"
length: 2
__proto__: Array[0]
__proto__: Object
1: Object
hints: Object
items: Array[2]
0: "Bio-conversion"
1: "[{"target": 2, "value": 0.597 },{ "target": 3, "value": 26.862 },{"target": 4, "value": 280.322 },{ "target": 5, "value": 81.144 }]"
length: 2

如何使用上面提供的数据获得预期的输出?

Where Agricultural 'waste = Source: 0,
      Bio-conversion = Source: 1,
      Liquid = Source: 2 and so on

0 个答案:

没有答案