JSON中需要的帮助

时间:2014-07-26 08:04:01

标签: javascript jquery json highcharts

我在json数据下面有这个。

[{"start_date":"2014-06-27","count":"21","value":"134"},{"start_date":"2014-06-28","count":"17","value":"120"},{"start_date":"2014-06-29","count":"21","value":"138"},{"start_date":"2014-06-30","count":"9","value":"121"},{"start_date":"2014-07-01","count":"12","value":"112"},{"start_date":"2014-07-02","count":"19","value":"132"}] 

我尝试使用highcharts绘制这个折线图。

我想将我的数据转换为这样的数据。

[{
   name: "count",
   data: [21, 17 .......]
},{
  name: "value",
  data: [134, 120, .......]
}]

如何使用jquery格式化我的数据?

我使用AJAX从数据库中获取数据。

任何帮助都会非常有用。

提前致谢。

3 个答案:

答案 0 :(得分:1)

使用

var res= YOUR JSON DATA
var data=$.parseJSON(res);
var count= new Array();
var val=new Array();
$(data).each(function(i,u){
    count.push(u.count);
    val.push(u.value);
});

now you can use 

[{
   name: "count",
   data: count
},{
  name: "value",
  data: val
}]

答案 1 :(得分:0)

var array = [{"start_date":"2014-06-27","count":"21","value":"134"},{"start_date":"2014-06-28","count":"17","value":"120"},{"start_date":"2014-06-29","count":"21","value":"138"},{"start_date":"2014-06-30","count":"9","value":"121"},{"start_date":"2014-07-01","count":"12","value":"112"},{"start_date":"2014-07-02","count":"19","value":"132"}]

var chartOptions = [];
var countData = {name: "count", data: []};
var valueData = {name: "value", data: []};

array.forEach(function(value, index){
  countData.data.push(value.count);
  valueData.data.push(value.value);
});

chartOptions.push(countData);
chartOptions.push(valueData);

答案 2 :(得分:0)

我将构建highcharts服务器端所需的JSON结构,然后在AJAX调用中返回它。只需将其直接添加到highcharts。

这将比用户浏览器进行格式化更有效。