我有一个JSON对象:
var info=JSON.parse(server_response);
我跑了console.log(info);
并得到了这个输出:
[ [ [ "Dipu", "Mondal", "O Positive", "017xxxx", "AIS" ] ], [ [ "dipu", "Roy", "O Positive", "017xxxx", "Electrical" ] ], [ [ "Dhinka", "Chika", "O Positive", "9038485777", "stat" ] ] ]
跟着这个小提琴:http://jsfiddle.net/6CGh8/我试过:
console.log(info.length); //output: 161 console.log(info[0].length); //output: 1 console.log(info[0][1]); //output: undefined
而这3条线的预期输出(分别):
3
5
Dipu
为什么我期待这个:
这个JSON对象包含3个数组,每个数组有5个数据,[0][1]
第th个元素是Dipu
.
如何获得预期的输出?
你有json编码两次,所以info只是一个文本而不是一个数组.
info.length // length of the string info[0].length // length of a character (1) info[0][1] // undefined because a character is not an array
尝试 var info = JSON.parse(JSON.parse(server_response))