我在Python中有一个JSON对象,它来自调用一个API (使用urllib2)的结果,如下所示:
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来做这件事吗?
更新1
根据贾丁的回答,我尝试了以下几点:
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发布于 2015-04-05 12:45:04
我认为您得到了这个错误,因为您的JSON中的一些值没有_typeGroup。试试这个:
for key,value in x.items():
if value.get("_typeGroup", "") == "entities":
print value['_type'], value['name']发布于 2015-04-05 10:57:06
for key,value in json1.items():
if value.get('typeGroup') == "entities":
print value.get('_type'), value.get('name')试试这个让我知道。应该管用的。
https://stackoverflow.com/questions/29456426
复制相似问题