简体   繁体   中英

Convert from JSON Array object to Array for highchart?

I'm developing a chart using highchart plugin. For giving dynamic input for chart. So, I need to change the JSON format to Array. how to change the JSON array object format to Array using javascript?

[
        {
            "scoreCount": "108",
            "legendDesc": "Excellent ",
            "colorPatchCode": "#009900"
        },
        {
            "scoreCount": "20",
            "legendDesc": "Fail ",
            "colorPatchCode": "#FF0000"
        }
    ]

to convert Array format like ,

[
        [
            "scoreCount": "108",
            "legendDesc": "Excellent ",
            "colorPatchCode": "#009900"
        ],
        [
            "scoreCount": "20",
            "legendDesc": "Fail ",
            "colorPatchCode": "#FF0000"
        ]
    ]

Can any one help?

[
            "scoreCount": "108",
            "legendDesc": "Excellent ",
            "colorPatchCode": "#009900"
        ],

is not valid structure. Array can contain single element. native , or object which contain another items(properties) , or array.

What if try this:

var arr = [
        {
            "scoreCount": "108",
            "legendDesc": "Excellent ",
            "colorPatchCode": "#009900"
        },
        {
            "scoreCount": "62",
            "legendDesc": "Good ",
            "colorPatchCode": "#99CC00"
        },
        {
            "scoreCount": "55",
            "legendDesc": "Acceptable ",
            "colorPatchCode": "#FFFF00"
        },
        {
            "scoreCount": "31",
            "legendDesc": "Poor ",
            "colorPatchCode": "#FF9900"
        },
        {
            "scoreCount": "20",
            "legendDesc": "Fail ",
            "colorPatchCode": "#FF0000"
        }
    ];

for (i = 0;i<arr.length;i++) {
    var a = [];
    for (n in arr[i]) {
        a[n] = arr[i][n];
    }
    arr[i] = a;
}

for (i = 0;i<arr.length;i++) {
    alert(arr[i]['scoreCount']);
}

http://jsfiddle.net/sPN7z/

var source = [
    {
        "scoreCount": "108",
        "legendDesc": "Excellent ",
        "colorPatchCode": "#009900"
    },
    {
        "scoreCount": "62",
        "legendDesc": "Good ",
        "colorPatchCode": "#99CC00"
    },
    {
        "scoreCount": "55",
        "legendDesc": "Acceptable ",
        "colorPatchCode": "#FFFF00"
    },
    {
        "scoreCount": "31",
        "legendDesc": "Poor ",
        "colorPatchCode": "#FF9900"
    },
    {
        "scoreCount": "20",
        "legendDesc": "Fail ",
        "colorPatchCode": "#FF0000"
    }
];

var out = [];

for (var i = 0; i < source.length; i++) {
    var tmp = [];
    for (var i2 in source[i]) {
        tmp.push(source[i][i2]);
    }
    out.push(tmp);
}

out would then be:

[
    ["108","Excellent ","#009900"],
    ["62","Good ","#99CC00"],
    ["55","Acceptable ","#FFFF00"],
    ["31","Poor ","#FF9900"],
    ["20","Fail ","#FF0000"]
]

as the others said already.. you cannot store key values in javascript arrays. thats what json is for.

Though the syntax is wrong, but if you want you can make this format of string anyway.

But, as you know, you cannot read it back to JavaScript variable.

You can use object oriented capabilities of javascript to accomplish this in more cleaner way.

var jsonArray = [
{
"scoreCount": "108",
"legendDesc": "Excellent ",
"colorPatchCode": "#009900"
},
{
"scoreCount": "62",
"legendDesc": "Good ",
"colorPatchCode": "#99CC00"
},
{
"scoreCount": "55",
"legendDesc": "Acceptable ",
"colorPatchCode": "#FFFF00"
},
{
"scoreCount": "31",
"legendDesc": "Poor ",
"colorPatchCode": "#FF9900"
},
{
"scoreCount": "20",
"legendDesc": "Fail ",
"colorPatchCode": "#FF0000"
}
];

function MyJsObject(scoreCount, legendDesc, colorPatchCode) {
this.scoreCount = scoreCount;
this.legendDesc = legendDesc;
this.colorPatchCode = colorPatchCode;
}

function createJSArray() {
var jsArray = new Array();
for(i = 0; i < jsonArray.length; i++) {
var m = new MyJsObject(jsonArray[i].scoreCount, jsonArray[i].legendDesc, jsonArray[i].colorPatchCode);
jsArray[i] = m;
}
alert(jsArray[4].scoreCount); //access any object like this
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM