我正在尝试从以下JSON中获取特定项目,但我不确定如何才能执行此操作。例如,我只是想在数组“collection1”中检索ID为“mondaymenu”的所有对象:
Object {name: "FF_1_menuitems", count: 223, frequency: "weekly", version: 7, newdata: true…}
count: 223
frequency: "weekly"
lastrunstatus: "success"
lastsuccess: "Fri Mar 28 2014 14:00:21 GMT+0000 (UTC)"
name: "FF_1_menuitems"
newdata: true
nextrun: "Fri Apr 04 2014 14:00:21 GMT+0000 (UTC)"
results: Object
collection1: Array[33]
0: Object
mondaymenu: "Bacon"
__proto__: Object
1: Object
mondaymenu: "Belgian Waffles"
__proto__: Object
我试过谷歌搜索,但无济于事。任何帮助将不胜感激!
答案 0 :(得分:0)
假设您有一个json对象json_data
,然后您可以使用
json_data.somekey
或
json_data['somekey']
在您的具体示例中,(假设您已分配了json_data),您可以使用
访问collection1
json_data.results.collection1 // or json_data['results']['collection1']
要在collection1
中找到ID为“mondaymenu”的所有对象,您可以执行以下操作
// loop through collection1
for (i = 0; i < json_data.results.collection1.length; i++) {
// if 'mondaymenu' isn't there, this will be null (false)
if (json_data.results.collection1[i]['mondaymenu']) {
// do something here
}
}