通过多级键解析JSON值

时间:2014-08-22 11:20:02

标签: python json python-2.7

昨天,我开始学习python。我想现在解析一些JSON值。我已经阅读了很多教程,花了很多时间在我的脚本中通过多级键获取值(如果我可以这样调用它),但对我来说没什么用。你能帮帮我吗?

这是我的JSON输出:

{
"future.arte.tv": [
    {
        "mediaUrl": "http://future.arte.tv/sites/default/files/styles/desktop-span12-940x529/public/berlin.jpg?itok=CvYlNekR",
        "micropost": {
            "html": "Berlin ",
            "plainText": "Berlin"
        },
        "micropostUrl": "http://future.arte.tv/de/der-erste-weltkrieg-die-rolle-von-wissenschaft-und-technik",
        "publicationDate": "Tue Jun 17 20:31:33 CEST 2014",
        "relevance": 5.9615083,
        "timestamp": 1403029893606,
        "type": "image"
    }
],
"www.zdf.de": [
    {
        "mediaUrl": "http://www.zdf.de/ZDFmediathek/contentblob/368/timg94x65blob/9800025",
        "micropost": {
            "plainText": "Berlin direkt"
        },
        "micropostUrl": "http://www.zdf.de/ZDFmediathek/hauptnavigation/sendung-a-bis-z",
        "publicationDate": "Tue Jun 10 16:25:42 CEST 2014",
        "relevance": 3.7259426,
        "timestamp": 1402410342400,
        "type": "image"
    }
]
}

我需要将值存储在" mediaUrl"关键,所以我试着做

j = json.loads(jsonOutput)
keys = j.keys(); 
for key in keys:
    print key   # keys are future.arte.tv and www.zdf.de
    print j[key]["mediaUrl"]

但是打印j [key] [" mediaUrl"]会导致此错误:

TypeError: list indices must be integers, not str

所以我试着打印j [key] [0],但结果不是我想要的(我想只有mediaUrl值... btw j [key] [1]导致列表索引出来范围错误):

{u'micropostUrl': u'http://www.berlin.de/special/gesundheit-und-beauty/ernaehrung/1692726-215-spargelhoefe-in-brandenburg.html', u'mediaUrl': u'http://berlin.de/binaries/asset/image_assets/42859/ratio_4_3/1371638570/170x130/', u'timestamp': 1403862143675, u'micropost': {u'plainText': u'Spargel', u'html': u'Spargel '}, u'publicationDate': u'Fri Jun 27 11:42:23 CEST 2014', u'relevance': 1.6377668, u'type': u'image'}
你能给我一些建议吗?

1 个答案:

答案 0 :(得分:2)

这是一个应该做的列表理解

>>> [d[i][0].get('mediaUrl') for i in d.keys()]
['http://www.zdf.de/ZDFmediathek/contentblob/368/timg94x65blob/9800025',
 'http://future.arte.tv/sites/default/files/styles/desktop-span12-940x529/public/berlin.jpg?itok=CvYlNekR']

工作原理

首先,您可以获得顶级密钥列表

>>> d.keys()
['www.zdf.de', 'future.arte.tv']

获取相应的值

>>> [d[i] for i in d.keys()]
[[{'micropostUrl': 'http://www.zdf.de/ZDFmediathek/hauptnavigation/sendung-a-bis-z', 'mediaUrl': 'http://www.zdf.de/ZDFmediathek/contentblob/368/timg94x65blob/9800025', 'timestamp': 1402410342400L, 'micropost': {'plainText': 'Berlin direkt'}, 'publicationDate': 'Tue Jun 10 16:25:42 CEST 2014', 'relevance': 3.7259426, 'type': 'image'}], [{'micropostUrl': 'http://future.arte.tv/de/der-erste-weltkrieg-die-rolle-von-wissenschaft-und-technik', 'mediaUrl': 'http://future.arte.tv/sites/default/files/styles/desktop-span12-940x529/public/berlin.jpg?itok=CvYlNekR', 'timestamp': 1403029893606L, 'micropost': {'plainText': 'Berlin', 'html': 'Berlin '}, 'publicationDate': 'Tue Jun 17 20:31:33 CEST 2014', 'relevance': 5.9615083, 'type': 'image'}]]

对于每个字典,请抓取'mediaUrl'

的值
>>> [d[i][0].get('mediaUrl') for i in d.keys()]
['http://www.zdf.de/ZDFmediathek/contentblob/368/timg94x65blob/9800025',
 'http://future.arte.tv/sites/default/files/styles/desktop-span12-940x529/public/berlin.jpg?itok=CvYlNekR']