How to change JSON Format For load data to Highchart

陌路散爱 提交于 2019-12-13 05:17:09

问题


My JSON looks like this:

[
    {
        "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
    }
]

And I want to load those data in Highchart but it needs another format:

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

The data is generated by database not in a single file like text.json but it directly generated.


回答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 now contains your data the way you want it =)




回答2:


your can create a javascript array of arrays and place the values as follows:

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];
   }
}



回答3:


You need to iterate through your JSON input and construct an Array dynamically. Then just use JSON.stringify() to get the desired output 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 );

Output:

[[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]]




回答4:


Try this out:- 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);
});


来源:https://stackoverflow.com/questions/18691194/how-to-change-json-format-for-load-data-to-highchart

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!