将JSON数组转换为JavaScript数组

时间:2014-03-12 11:21:59

标签: javascript json

我正在尝试转换json数组

                {"id":"1","name":"abc"},
                {"id":"2","name":"pqr"},
                {"id":"3","name":"xyz"};

进入这种js数组

 var locations = [
    [1, 'abc'],
    [2, 'pqr'],
    [3, 'xyz']
];

2 个答案:

答案 0 :(得分:1)

我假设你有元素对象,(而不是你所说的Json):

var data = [{"id":"1","name":"abc"},
            {"id":"2","name":"pqr"},
            {"id":"3","name":"xyz"}];

你可以把它转换成像这样的二维数组

var output = new Array();
for (var i = 0; i < data.length; i++) {
    output[i] = new Array(data[i].id, data[i].name);
}

答案 1 :(得分:0)

你可以这样做。

var jsondata=[{"id":"1","name":"abc"},
                {"id":"2","name":"pqr"},
                {"id":"3","name":"xyz"}];
var arrayObj=$.parseJSON(jsondata);