2015-04-05 108 views
0

我已经从呼叫的结果在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 
+0

Python中的json对象只是另一个字典。你知道如何访问字典的项目吗? – 2015-04-05 10:48:36

+0

我是Python的基本用户。我可以通过使用json1 [“http://d.opencalais.com/genericHasher-1/802a1ebb-7fac-354f-b02f-6ef8442950d3”]来访问它。但是,我想通过循环每个键并检查条件嵌套键来完成。不知道该怎么做。 – Ravi 2015-04-05 10:53:31

+1

我只是试图提高你的问题处理技能。所以你知道你想用*循环*遍历*键*。 Python字典提供*方法*来获取所有的密钥,所以你不必手动输入它们。你可以查看关于'dict.keys()'的文档,或者更简单的'键入字典:'。 – 2015-04-05 10:57:06

回答

1

我想你会得到这个错误,因为你的JSON中的某些值没有_typeGroup。试试这个:

for key,value in x.items(): 
    if value.get("_typeGroup", "") == "entities": 
     print value['_type'], value['name'] 
+0

这工作得很好。你能否解释一下value.get(“_ typeGroup”,“”)实际上做了什么? – Ravi 2015-04-05 12:57:19

+1

@Ravi如果它存在,它会从字典中获取与_typeGroup对应的字典中的元素,否则它将采用缺省值,这是第二个位置参数'“”'给出的缺省值,这里是空字符串。我认为贾廷的回答会引导你到那里去,所以你应该考虑加强他的回答,以及他不可能知道并非所有的价值都存在。 – 2015-04-05 13:02:36

+1

当然,'get'方法可以让你返回一个默认值,如果你找的键没有在字典中找到([documentation](https://docs.python.org/2/library/stdtypes。 HTML#dict.get))。如果没有指定缺省值,我也发现它返回'None',所以上面可能只是'value.get(“_ typeGroup”)' – 2015-04-05 13:02:57

2
for key,value in json1.items(): 
    if value.get('typeGroup') == "entities": 
     print value.get('_type'), value.get('name') 

试试这个,让我知道。 IT应该工作。

+0

这会引发错误:KeyError:'_typeGroup'。我已更新我的原始帖子,更多信息 – Ravi 2015-04-05 12:31:13