无需循环即可直接访问JSON中的数组值

时间:2014-01-23 22:16:22

标签: javascript arrays json

我有以下JSON:

{
  "responseHeader":{
    "params":{
      "facet":"true",
      "fl":"city",
      "facet.mincount":"1",
      "indent":"on",
      "start":"0",
      "q":["*:*",
        "*:*"],
      "wt":"json",
      "rows":"12"}},
  "response":{"numFound":1,"start":0,"docs":[
      {"city":"lathum"}]
  },
  "facet_counts":{
    "facet_fields":{
      "hasphoto":[
        "true",61,
        "false",5],
      "hasvideo":[
        "false",51,
        "true",15],
      "rating_rounded":[
        "0.0",62,
        "10.0",3,
        "8.0",1]},
    "facet_ranges":{}}}

我想知道是否可以根据属性名称选择一个值,在我的情况下,我想选择有多少hasphoto具有值true,这将是61。 请注意,true值不一定必须是hasphoto中列出的第一个值,truefalse按出现次数排序。

我想直接获取值,而不必循环遍历它......可能吗?

我试过了:

response.facet_counts.facet_fields['hasphoto']['true']

response.facet_counts.facet_fields.hasphoto['true']

但两者都返回undefined

1 个答案:

答案 0 :(得分:1)

数组是有序列表,而不是键值存储,所以

response.facet_counts.facet_fields.hasphoto[0]为您提供字符串“true”, response.facet_counts.facet_fields.hasphoto[1]为您提供了数字61, 等...