我试图使用d3.json()格式化我的json文件。谁能帮我

时间:2014-09-03 09:24:20

标签: d3.js drupal-6

使用d3.json如何从这种格式转换

[{" HANDLERID":" AET55V8366"," HANDLER_MODEL":" AETRIUM 55V8", " START1":" 2014-03-30 00:00:00"," END1":" 2014-06-17 02:44:11& #34;" VAL1":" 49.7950&#34 ;, " START2":" 2014-06-17 02:44:11"," END2":" 2014-07-18 18:13:51& #34;" VAL2":" 69.7130&#34 ;, " START3":" 2014-07-18 18:13:51"," END3":" 2014-09-04 05:28:22& #34;" VAL3":" 99.00"},

{" HANDLERID":" AET55V8441"," HANDLER_MODEL":" AETRIUM 55V8", " START1":" 2014-02-19 00:00:00"," END1":" 2014-05-28 05:23:15& #34;" VAL1":" 50.0350&#34 ;, " START2":" 2014-05-28 05:23:15"," END2":" 2014-07-06 12:20:33& #34;" VAL2":" 70.0490&#34 ;, " START3":" 2014-07-06 12:20:33"," END3":" 2014-09-03 10:46:30& #34;" VAL3":" 100.00"},

{" HANDLERID":" AET55V8445"," HANDLER_MODEL":" AETRIUM 55V8", " START1":" 2014-06-14 00:00:00"," END1":" 2014-07-23 09:36:38& #34;" VAL1":" 51.7700&#34 ;, " START2":" 2014-07-23 09:36:38"," END2":" 2014-08-08 03:51:17& #34;" VAL2":" 72.4780&#34 ;, " START3":" 2014-08-08 03:51:17"," END3":" 2014-08-31 19:13:17& #34;" VAL3":" 103.00"},

这种格式???????????

var tasks = [ {" startDate":新日期(" 2014-03-30 00:00:00")," endDate":新日期(" 2014- 06-17 02:44:11")," taskName":" AET55V8366"," status":49.7950}, {" startDate":新日期(" 2014-06-17 02:44:11")," endDate":新日期(" 2014- 07-18 18:13:51")," taskName":" AET55V8366"," status":69.7130}, {" startDate":新日期(" 2014-07-18 18:13:51")," endDate":新日期(" 2014- 09-04 05:28:22")," taskName":" AET55V8366"," status":99.00},

{" startDate":新日期(" 2014-02-19 00:00:00")," endDate":新日期(" ; 2014-05-28 05:23:15")," taskName":" AET55V8441"," status":50.0350}, {" startDate":新日期(" 2014-05-28 05:23:15")," endDate":新日期(" 2014- 07-06 12:20:33")," taskName":" AET55V8441"," status":70.0490}, {" startDate":新日期(" 2014-07-06 12:20:33")," endDate":新日期(" 2014- 09-03 10:46:30")," taskName":" AET55V8441"," status":100.00},

{" startDate":新日期(" 2014-06-14 00:00:00")," endDate":新日期(" ; 2014-07-23 09:36:38")," taskName":" AET55V8445"," status":51.7700}, {" startDate":新日期(" 2014-07-23 09:36:38")," endDate":新日期(" 2014- 08-08 03:51:17")," taskName":" AET55V8445"," status":72.4780}, {" startDate":新日期(" 2014-08-08 03:51:17")," endDate":新日期(" 2014- 08-31 19:13:17")," taskName":" AET55V8445"," status":103.00}, ]。

2 个答案:

答案 0 :(得分:0)

var my=[]; 
d3.json("data.json", function(data){ data.forEach(function(d){ my.push(d);}); });
var result=[];
my.forEach(function(d, i){
result.push({"startDate": (d3.time.format("%Y-%m-%d %X").parse(my[i].START1)),
             "endDate": (d3.time.format("%Y-%m-%d %X").parse(my[i].END1)),  
             "taskName": my[i].HANDLERID,  
             "status": my[i].VAL1});
result.push({"startDate": (d3.time.format("%Y-%m-%d %X").parse(my[i].START2)),
             "endDate": (d3.time.format("%Y-%m-%d %X").parse(my[i].END2)),  
             "taskName": my[i].HANDLERID,  
             "status": my[i].VAL2});
result.push({"startDate": (d3.time.format("%Y-%m-%d %X").parse(my[i].START3)),
             "endDate": (d3.time.format("%Y-%m-%d %X").parse(my[i].END3)),  
             "taskName": my[0].HANDLERID,  
             "status": my[i].VAL3});
});
console.log(result);

结果数组是您要查找的数组。 希望它能帮到你。

data.json是包含问题和观察结果数组中上述数据格式的文件。

答案 1 :(得分:0)

目前还不清楚你想在这做什么。我可以看到你想要提取开始和结束日期和时间,但有3个选择,所以我只选择1.此外,你应该知道你不能在json对象中创建新的日期对象。您需要在外部执行此操作并将其传递到对象中。

无论如何,你应该能够使用下面的代码实现你的目标(这与saikiran发布的概念类似,只是更紧凑一些(而且我懒得解析三者中的每一个)时间对象)。

d3.json("rawJson.json", function (error, json){
    console.log(json)
    var newJson = [];
    var timeFormat = d3.time.format("%Y-%m-%d %H:%M:%S");

    json.forEach(function (d,i) {
        el = {
            "startDate": timeFormat.parse(d.START1),
            "endDate": timeFormat.parse(d.END1),
            "taskName": d.HANDLERID,
            "status": d.VAL1
        }
        newJson.push(el)
    })
console.log(newJson);
})

最后一件事,你发布的json很难读。我建议使用像jsonlint这样的linter服务。它使你的json更具人性化,并检查结构错误。