我已經從呼叫的結果在Python JSON對象到一個API(使用的urllib2)生成如下:提取從JSON特定值在Python
results = urllib2.urlopen(req).read()
json1 = json.loads(results)
此生成包含類似的東西JSON對象以下(由於截斷大小):
"http://d.opencalais.com/dochash-1/895ba8ff-4c32-3ae1-9615-9a9a9a1bcb39/cat/1":{
"_typeGroup":"topics",
"category":"http://d.opencalais.com/cat/Calais/Entertainment_Culture",
"classifierName":"Calais",
"categoryName":"Entertainment_Culture",
"score":1
},
"http://d.opencalais.com/genericHasher-1/b6a2d07d-133b-35ad-85e2-54d524e750cf":{
"_typeGroup":"entities",
"_type":"TVShow",
"name":"Hard Knocks",
"_typeReference":"http://s.opencalais.com/1/type/em/e/TVShow",
"instances":[
{
"detection":"[ New York Jets during the summer of 2010 on HBO's ]Hard Knocks[.\n]",
"prefix":" New York Jets during the summer of 2010 on HBO's ",
"exact":"Hard Knocks",
"suffix":".\n",
"offset":135,
"length":11
}
],
"relevance":0.5
},
"http://d.opencalais.com/genericHasher-1/802a1ebb-7fac-354f-b02f-6ef8442950d3":{
"_typeGroup":"entities",
"_type":"Organization",
"name":"New York Jets",
"organizationtype":"sports",
"nationality":"American",
"_typeReference":"http://s.opencalais.com/1/type/em/e/Organization",
"instances":[
{
"detection":"[ Tebow caught a few training camp glimpses of the ]New York Jets[ during the summer of 2010 on HBO's Hard]",
"prefix":" Tebow caught a few training camp glimpses of the ",
"exact":"New York Jets",
"suffix":" during the summer of 2010 on HBO's Hard",
"offset":86,
"length":13
}
],
"relevance":0.5
}
從這個JSON,我想提取「_type」和「名」僅在「typeGroup」 ==「實體」。
例如,對於上面的JSON對象的輸出應該是這樣的:
TVShow: Hard Knocks
Organization: New York Jets.
可能有人請就如何做到這一點在Python幫助嗎?
[UPDATE 1]
基於來自Jatin答案我嘗試以下:
for key,value in json1.items():
if value["_typeGroup"] == "entities":
print value['_type'], value['name']
然而,這導致錯誤KeyError異常: '_typeGroup'
我試圖看如何按鍵和值打印如下:
for key,value in json1.items():
print key,value
這導致下面的輸出(表示只是一個鍵,值對):
http://d.opencalais.com/genericHasher-1/802a1ebb-7fac-354f-b02f-6ef8442950d3 {u'_typeReference': u'http://s.opencalais.com/1/type/em/e/Organization', u'_type': u'Organization', u'name': u'New York Jets', u'_typeGroup': u'entities', u'instances': [{u'suffix': u" during the summer of 2010 on HBO's Hard", u'prefix': u' Tebow caught a few training camp glimpses of the ', u'detection': u"[ Tebow caught a few training camp glimpses of the ]New York Jets[ during the summer of 2010 on HBO's Hard]", u'length': 13, u'offset': 86, u'exact': u'New York Jets'}], u'relevance': 0.5, u'nationality': u'American', u'organizationtype': u'sports'}
這似乎是一個嵌套JSON。所以我嘗試了以下按如下方式訪問內部鍵值對:
for key,value in json1.items():
val1 = value
for key,value in val1.items():
if value["_typeGroup"] == "entities":
print value['_type'], value['name']
然而,它引發以下錯誤:
TypeError: string indices must be integers
Python中的json對象只是另一個字典。你知道如何訪問字典的項目嗎? – 2015-04-05 10:48:36
我是Python的基本用戶。我可以通過使用json1 [「http://d.opencalais.com/genericHasher-1/802a1ebb-7fac-354f-b02f-6ef8442950d3」]來訪問它。但是,我想通過循環每個鍵並檢查條件嵌套鍵來完成。不知道該怎麼做。 – Ravi 2015-04-05 10:53:31
我只是試圖提高你的問題處理技能。所以你知道你想用*循環*遍歷*鍵*。 Python字典提供*方法*來獲取所有的密鑰,所以你不必手動輸入它們。你可以查看關於'dict.keys()'的文檔,或者更簡單的'鍵入字典:'。 – 2015-04-05 10:57:06