我已经做了很多搜索,但我似乎无法理解如何转换它:
[
{"id":749,"time":20160416162403,"value":707},
{"id":750,"time":20160416162407,"value":708},
{"id":751,"time":20160416162411,"value":703},
{"id":752,"time":20160416162415,"value":710}
]
进入HighCharts或GoogleCharts可以使用的东西,如系列:
name: []
time: [time1, time2, time3]
我已经看过使用JQuery的示例,但我似乎无法让它运行起来。如果有人能提供快速示例/解决方案,我可以从那里运行它。
感谢编辑GG,我下次会尝试正确格式化。
答案 0 :(得分:3)
我认为你可以为它构建一个简单的构造函数:
var jsonObj = [
{"id":749,"time":20160416162403,"value":707},
{"id":750,"time":20160416162407,"value":708},
{"id":751,"time":20160416162411,"value":703},
{"id":752,"time":20160416162415,"value":710}
]
var highChartsObj = {
time: [],
value: []
}
$.each( jsonObj , function( key, val ) {
highChartsObj.time.push(val.time)
highChartsObj.value.push(val.value)
});
答案 1 :(得分:2)
与杰弗里的方法相同,只使用纯粹的javascript(略微不合适的方式)。
var sensorOutput = [
{"id":749,"time":20160416162403,"value":707},
{"id":750,"time":20160416162407,"value":708},
{"id":751,"time":20160416162411,"value":703},
{"id":752,"time":20160416162415,"value":710}
];
//Here I declare an output class;
var outputClass = function()
{
this.name='';
this.time = [];
};
function convertOutput(inputArray)
{
//Here I instantiate the class
var output = new outputClass();
//And here I populate its array with the input received from the sensors.
for(var x=0; x< inputArray.length; x++)
{
var sensorOutputLine = inputArray[x];
var currentTime = sensorOutputLine.time;
output.time.push(currentTime); //Push the item into the time[] array
}
}
//Function call
convertOutput(sensorOutput);
这是我第一次提出的代码。如果您正在考虑使用它,请考虑改进它:)
JsBin在这里:https://jsbin.com/mixasihibe/edit?html,js,output
编辑:提高了代码的可读性......无聊无聊意味着xD