2016-07-26 82 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