3 回答
TA贡献1790条经验 获得超9个赞
以下
data = [{
"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"
}
}
]
}
}
]
messages = [x['hits']['hits'][0]['_source']['message'] for x in data]
print(messages)
输出
['Error something happened', 'Something else']
TA贡献1797条经验 获得超4个赞
尝试这个
res = [y['_source']['message'] for x in data for y in x['hits']['hits']]
print(res)
输出:
['Error something happened', 'Something else']
TA贡献1802条经验 获得超6个赞
一个简单的 for 循环就可以解决问题:
for d in data :
extracted_data.append(d['hits']['hits'][0]['_source']['message'])
添加回答
举报