简体   繁体   English

使用JavaScript将JSON Array文件转换为图形

[英]Covert JSON Array file into graph using JavaScript

I am grabbing a JSON file from a server which has the following format: 我正在从具有以下格式的服务器中获取JSON文件:

[{"target": "carbon.agents.graphite01TimeTemp",
  "datapoints": 
    [  
       [0.00034302131648684774, 1395792060],
       [0.00041960148485072106, 1395792120],
       [0.00035245191970241269, 1395792180], 
       [0.00032920416076246247, 1395792240],
       ...
       [0.00034535436076246232, 1395792546]
    ]
}]

I am grabbing it using a node.js request using this library. 我正在使用使用库的node.js请求来抓取它。

This is my full code: 这是我的完整代码:

var request = require('request');
request('###grabthejsondataurl###', function (error, response, body) {
    if (!error && response.statusCode == 200) {
        var returnval= JSON.parse(body);
            console.log(body);
        config.temp = returnval.datapoints;
        console.log(config.temp);
        job_callback(null, config);
    } 
})

The console.log(body) print out the JSON I displayed above. console.log(body)打印出我上面显示的JSON。

But the console.log(config.temp) print out undefined . 但是console.log(config.temp)打印出undefined

Anybody understand why this is happening??? 有人知道为什么会这样吗???

Extra Information: 额外的信息:

The values on the left (0.0003342 etc) are temperatures and the values on the right (1395 etc) are the times. 左边的值(0.0003342等)是温度,右边的值(1395等)是时间。

The plan is to get all this data into an array in JavaScript then convert it into a graph. 计划是将所有这些数据放入JavaScript中的数组中,然后将其转换为图形。

Thanks for taking a look! 谢谢参观!

The data object is contained within an array. 数据对象包含在数组中。 You can access it with: returnval[0].datapoints . 您可以使用以下命令访问它: returnval[0].datapoints

Try: 尝试:

var request = require('request');
request('###grabthejsondataurl###', function (error, response, body) {
    if (!error && response.statusCode == 200) {
        var returnval= JSON.parse(body);
            console.log(body);
        config.temp = returnval[0].datapoints;
        console.log(config.temp);
        job_callback(null, config);
    } 
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM