从以下JSON获取特定数据?

哈莎·库尚迪(Harsha Kuchampudi)

问题

我正在尝试从以下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

我已经尝试了Google搜寻,但无济于事。任何帮助,将不胜感激!

乔什·达勒姆(Josh Durham)

假设您有一个json对象json_data,那么您可以使用来访问其中的数据

json_data.somekey

或者

json_data['somekey']

在您的特定示例中(假设您已分配json_data),您可以collection1使用

json_data.results.collection1 // or json_data['results']['collection1']

要找到所有ID为“ mondaymenu”的对象,collection1您可以执行以下操作

// 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

  }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章