我有以下JSON:
{
_id:"7654567Bfyuhj678",
result:{
CAR:[
[
"myCar1",
12233
],
[
"myCar2",
2343
],
[
"myCar3",
5435
]
],
BUS:[
[
"Bus1",
AAE33
]
],
TAXI:[
[
"myTaxi1",
463789
],
[
"myTaxi2",
543
],
[
"myTaxi3",
5445
]
]
}
}
问题是我事先不知道" QWERTY "," PML ",& #34;的 TAXI "但是我需要用散列表QWERTY myCar1 12233,myCar2 2343 PML Bus1,AAE33来获取数据....
如何使用JavaScript进行操作?
$http.get('http://localhost:3000/transports/'+id)
.success(function (transport) {
console.log("transport: ", transport);
})
.error(function (transport) {
console.log(transport);
});
JSON位于transport
。
感谢您的帮助!
答案 0 :(得分:2)
JSON对象与普通对象几乎没有任何区别,并且可以以完全相同的方式遍历,即using a loop:
.success(function (transport) {
for ( var idx in transport.result ) {
// idx == 'CAR', then idx == 'BUS' etc.
// transport.result[idx] = [ ["myCar1", 12223], ["myCar2", ...], ... ], etc.
doStuffWith(transport.result[idx]);
}
}