2014-01-23 120 views
0

我有以下的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

+0

你或许应该指出哪些语言你正在使用。 FORTRAN中的JSON支持非常神秘。 –

回答

1

数组是有序列表,而不是键值存储这样

response.facet_counts.facet_fields.hasphoto[0]给你字符串“true”, response.facet_counts.facet_fields.hasphoto[1]给你的号码61, 等等

+0

嗯,好的,所以我尝试了'inArray'这个'console.log('index of true:'+ $ .inArray('true',data.facet_counts.facet_fields ['hasphoto']));'但是返回' 2' ...你知道为什么吗? – Flo