如何更改JSON格式将数据加载到Highchart

时间:2013-09-09 03:40:01

标签: javascript json highcharts

我的JSON看起来像这样:

[
    {
        "1332879360000.0": 300,
        "1332797760000.0": 353,
        "1332799320000.0": 358,
        "1332879780000.0": 302,
        "1332800160000.0": 359,
        "1332880200000.0": 299,
        "1332880620000.0": 298,
        "1332881040000.0": 301,
        "1332881460000.0": 402,
        "1332880020000.0": 330,
        "1332882300000.0": 466,
        "1332796620000.0": 519,
        "1332800520000.0": 447,
        "1332797460000.0": 359,
        "1332801000000.0": 442
    }
]

我想在Highchart中加载这些数据,但它需要另一种格式:

[
    [
        1332879360000,
        300
    ],
    [
        1332797760000,
        353
    ],
    [
        1332799320000,
        358
    ],
    [
        1332879780000,
        302
    ]
]

数据是由数据库生成的,不是像text.json这样的单个文件,而是直接生成的。

4 个答案:

答案 0 :(得分:1)

json1 = [{"1332879360000.0": 300.0, "1332797760000.0": 353.0, "1332799320000.0": 358.0, "1332879780000.0": 302.0, "1332800160000.0": 359.0, "1332880200000.0": 299.0, "1332880620000.0": 298.0, "1332881040000.0": 301.0, "1332881460000.0": 402.0, "1332880020000.0": 330.0, "1332882300000.0": 466.0, "1332796620000.0": 519.0, "1332800520000.0": 447.0, "1332797460000.0": 359.0, "1332801000000.0": 442.0}];

hc = []

for(key in json1[0])
{
    hc.push([key,json1[0][key]])
}

json2 = JSON.stringify(hc)

json2现在按您希望的方式包含您的数据=)

答案 1 :(得分:0)

你可以创建一个javascript数组数组,并按如下方式放置值:

var json = ... your json array

// construct your java-script array
var value = new Array(json.length);

// populate javascript array
for (var n = 0; n < json.length; n++ ) {
   var obj = json[n];
   value[n] = new Array(2);
   for(var key in obj){
        value[0] = key;
        value[1] = obj[key];
   }
}

答案 2 :(得分:0)

您需要遍历JSON输入并动态构造数组。然后只需使用JSON.stringify()即可获得所需的输出JSON。

var jsonIn ="[\r\n" + 
        "    {\r\n" + 
        "        \"1332879360000.0\": 300,\r\n" + 
        "        \"1332797760000.0\": 353,\r\n" + 
        "        \"1332799320000.0\": 358,\r\n" + 
        "        \"1332879780000.0\": 302,\r\n" + 
        "        \"1332800160000.0\": 359,\r\n" + 
        "        \"1332880200000.0\": 299,\r\n" + 
        "        \"1332880620000.0\": 298,\r\n" + 
        "        \"1332881040000.0\": 301,\r\n" + 
        "        \"1332881460000.0\": 402,\r\n" + 
        "        \"1332880020000.0\": 330,\r\n" + 
        "        \"1332882300000.0\": 466,\r\n" + 
        "        \"1332796620000.0\": 519,\r\n" + 
        "        \"1332800520000.0\": 447,\r\n" + 
        "        \"1332797460000.0\": 359,\r\n" + 
        "        \"1332801000000.0\": 442\r\n" + 
        "    }\r\n" + 
        "]";

var jsonArr = [];       
var jsonObj = JSON.parse( jsonIn )[0];

for( var key in jsonObj ) {
    jsonArr.push( [parseInt( key ), jsonObj[key]] );
}

var jsonOut = JSON.stringify( jsonArr ); 
console.log( jsonOut );

输出:

  

[[1332879360000,300],[1332797760000,353],[1332799320000,358],[1332879780000,302],[1332800160000,359],[1332880200000,299],[1332880620000,298],[1332881040000,301 ],[1332881460000,402],[1332880020000,330],[1332882300000,466],[1332796620000,519],[1332800520000,447],[1332797460000,359],[1332801000000,442]]

答案 3 :(得分:0)

试试这个: - http://jsfiddle.net/adiioo7/tcu8t/2/

<强> JS: -

var oldFormat = [{
    "1332879360000.0": 300,
        "1332797760000.0": 353,
        "1332799320000.0": 358,
        "1332879780000.0": 302,
        "1332800160000.0": 359,
        "1332880200000.0": 299,
        "1332880620000.0": 298,
        "1332881040000.0": 301,
        "1332881460000.0": 402,
        "1332880020000.0": 330,
        "1332882300000.0": 466,
        "1332796620000.0": 519,
        "1332800520000.0": 447,
        "1332797460000.0": 359,
        "1332801000000.0": 442
}];

var newFormat = [];

jQuery(function ($) {
    $.each(oldFormat[0], function (i, item) {
        newFormat.push([parseInt(i), item]);
    });

    console.log(newFormat);
});