3

我正在使用 highchart 插件开发图表。用于为图表提供动态输入。因此,我需要将 JSON 格式更改为 Array。如何使用 javascript 将 JSON 数组对象格式更改为 Array?

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

转换数组格式,如

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

任何人都可以帮忙吗?

4

5 回答 5

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

不是有效的结构。数组可以包含单个元素。native 或包含另一个项目(属性)或数组的对象。

于 2012-10-25T12:54:25.607 回答
1

如果试试这个怎么办:

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']);
}
于 2012-10-25T13:12:08.030 回答
0

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

然后将是:

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

正如其他人已经说过的那样..您不能将键值存储在javascript数组中。这就是 json 的用途。

于 2012-10-25T13:08:42.620 回答
0

您可以使用 javascript 的面向对象功能以更简洁的方式完成此任务。

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
}

于 2012-10-26T04:22:42.033 回答
0

虽然语法是错误的,但如果你愿意,无论如何你都可以制作这种格式的字符串。

但是,如您所知,您无法将其读回 JavaScript 变量。

于 2012-10-25T15:00:22.017 回答