0

我的 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

4 回答 4

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 现在以您想要的方式包含您的数据 =)

于 2013-09-09T04:05:52.450 回答
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);
});
于 2013-09-09T04:29:12.903 回答
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],[13328001600160000,359] 1332881460000,402],[1332880020000,330],[1332882300000,466],[1332796620000,519],[1332800520000,447],[1332797460000,309],[13320]]

于 2013-09-09T04:04:50.147 回答
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];
   }
}
于 2013-09-09T03:58:30.600 回答