将多维JSON数组转换为JS变量

时间:2012-10-19 22:26:07

标签: javascript jquery json multidimensional-array

假设你有以下JSON:

{"zero":{"0":"foo","1":"foo2"},"one":"fooa","two":"foob"}

将其转换为最有效的方式是:

 var zero = ['foo','foo2'];
 var one = 'fooa';
 var two = 'foob';

1 个答案:

答案 0 :(得分:3)

JSON.parse

var json = '{"zero":{"0":"foo","1":"foo2"},"one":"fooa","two":"foob"}';
var pJson = JSON.parse(json);
var zero = [ pJson.zero[0], pJson.zero[1] ];
var one = pJson.one;
var two = pJson.two;