我正在尝试为列表中的每个对象提取一个特定的字段,它在 JSON 中的值,到目前为止,我已经开始一个一个地获取它们,但是如果有更多的对象具有相同的字段,它需要提取所有这些字段被添加到 JSON 中。
这是 JSON:
[{
"took" : 1023,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 1,
"max_score" : 114.88808,
"hits" : [
{
"_index" : 1,
"_type" : "doc",
"_id" : 1,
"_score" : 114.88808,
"_source" : {
"message" : "Error something happened"
}
}
]
}
},
{
"took" : 1023,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 1,
"max_score" : 114.88808,
"hits" : [
{
"_index" : 2,
"_type" : "doc",
"_id" : 2,
"_score" : 114.88808,
"_source" : {
"message" : "Something else"
}
}
]
}
}
]
我正在尝试从两个对象的字段中获取值message,就我提到的而言,我设法像这样一个一个地完成:
data = json.loads(open('test.json').read())
extracted_data = data[0]['hits']['hits'][0]['_source']['message']
弑天下
富国沪深
繁星coding
呼啦一阵风
随时随地看视频慕课网APP
相关分类