2016-07-26 145 views
0

我的代码如下:使用Python和elasticsearch,我如何循环返回的JSON对象?

import json 
from elasticsearch import Elasticsearch 

es = Elasticsearch() 

resp = es.search(index="mynewcontacts", body={"query": {"match_all": {}}}) 
    response = json.dumps(resp) 
    data = json.loads(response) 
    #print data["hits"]["hits"][0]["_source"]["email"] 
    for row in data: 
    print row["hits"]["hits"][0]["_source"]["email"] 
    return "OK" 

产生这种截断(为方便起见)JSON:当我试图

{"timed_out": false, "took": 1, "_shards": {"successful": 5, "total": 5, "failed": 0}, "hits": {"max_score": 1.0, "total": 7, "hits": [{"_index": "mynewcontacts", "_type": "contact", "_score": 1.0, 
"_source": {"email": "[email protected]", "position": "Sr.Researcher", "last": "Zhuo", "first": "Sharon", "company": "Tabridge Executive Search"}, "_id": "AVYmLMlKJVSAh7zyC0xf"}, 
{"_index": "mynewcontacts", "_type": "contact", "_score": 1.0, "_source": {"email": "[email protected]", "position": "Vice President", "last": "Springthorpe", "first": "Andrew", "company": "SBC Group"}, "_id": "AVYmLMlRJVSAh7zyC0xg"}, {"_index": "mynewcontacts", "_type": "contact", "_score": 1.0, "_source": {"email": "[email protected]", "position": "Financial Advisor", "last": "Bell", "first": "Margaret Jacqueline", "company": "Streamline"}, "_id": "AVYmLMlXJVSAh7zyC0xh"}, {"_index": "mynewcontacts", "_type": "contact", "_score": 1.0, "_source": {"email": "[email protected]", "position": "Technical Solutions Manager MMS North Asia", "last": "Okai", "first": "Kensuke", "company": "Criteo"}, "_id": "AVYmLMlfJVSAh7zyC0xi"}, {"_index": "mynewcontacts", "_type": "contact", "_score": 1.0, "_source": {"email": "[email protected]", "position": "Sr. Strategic Account Executive", "last": "Kato", "first": "Mizuto", "company": "Twitter"}, "_id": "AVYmLMlkJVSAh7zyC0xj"}, {"_index": "mynewcontacts", "_type": "contact", "_score": 1.0, "_source": {"email": "[email protected]", "position": "Design Manager", "last": "Okada", "first": "Kengo", "company": "ON Semiconductor"}, "_id": "AVYmLMlpJVSAh7zyC0xk"}, {"_index": "mynewcontacts", "_type": "contact", "_score": 1.0, "_source": {"email": "[email protected]", "position": "Legal Counsel", "last": "Lei", "first": "Yangzi (Karen)", "company": "Samsung China Semiconductor"}, "_id": "AVYmLMkUJVSAh7zyC0xe"}]}} 

print data["hits"]["hits"][0]["_source"]["email"] 

它打印的第一封电子邮件罚款当我尝试循环与

for row in data: 
    print row["hits"]["hits"][0]["_source"]["email"] 

我收到一个错误:

TypeError: string indices must be integers 

请可有人建议如何我可以通过正确的项目重复?非常感谢!

回答

1

我可能是错的,但看起来你可能不会根据正确的json项目启动for循环。尝试:

for row in data['hits']['hits']: 
    # Rest of loop here. 
+0

谢谢你,完美的作品。但为什么? – user1903663

+0

在你原来的代码中,你已经检索了一个字典键太深的JSON数据。 – drsnark

0

你的检索响应data是一个Python字典 - 如果你犯了一个for遍历它,它就会产生字典键 - 在这种情况下,德strigns timed_outtookshards,等...

实际上,yu想要在您的响应数据中迭代位置data["_shards"]["hits"]["hits"]中提供的列表。这是一个列表。

所以,只是做

for row in data["_shards"]["hits"]["hits"]: 
    print(row["_source"]["email"]) 
+0

谢谢。错误是:KeyError:点击 – user1903663

2

你在做什么,通过字典键循环。要打印每个电子邮件的响应,你会这样做:

for row in data["hits"]["hits"]: 
    print row["_source"]["email"] 

也转换为json是没有必要的。这应该完成你想要做的事情:

from elasticsearch import Elasticsearch 

es = Elasticsearch() 

resp = es.search(index="mynewcontacts", body={"query": {"match_all": {}}}) 
for row in resp["hits"]["hits"]: 
    print row["_source"]["email"] 
return "OK" 
+0

非常感谢您的洞察力。 – user1903663