我有以下JSON字符串,我想获得doc_count
:
import json
text = '''{
"took": 1,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 11,
"max_score": 0,
"hits": []
},
"aggregations": {
"range": {
"buckets": [
{
"key": "2017-02-17T15:00:00.000Z-2017-02-17T16:00:00.000Z",
"from": 1487343600000,
"from_as_string": "2017-02-17T15:00:00.000Z",
"to": 1487347200000,
"to_as_string": "2017-02-17T16:00:00.000Z",
"doc_count": 2
}
]
}
}
}'''
obj = json.loads(text)
obj
我尝试了obj['aggregations']['range']['buckets']['doc_count']
,但它不起作用。如何搜索此JSON文件的层次结构?
答案 0 :(得分:1)
您错过了"buckets"
键包含列表的事实。你需要:
obj['aggregations']['range']['buckets'][0]['doc_count']
选择存储桶密钥后注意[0]
。
答案 1 :(得分:1)
因为'buckets'
键包含元素数组。你需要
obj['aggregations']['range']['buckets'][0]['doc_count']