通过多级键解析JSON值 [英] parse JSON values by multilevel keys

查看:118
本文介绍了通过多级键解析JSON值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

昨天,我开始学习python.我现在想解析一些JSON值.我已经阅读了许多教程,并花了很多时间在脚本中通过多级键(如果可以这样称呼)获取值,但是对我没有任何作用.你能帮我吗?

Yesterday, I have started with learning python. I want to parse some JSON values now. I have read many of tutorials and spent a lot of time on getting values by multilevel key (if I can call it like that) in my script but nothing works to me. Can you help me please?

这是我的JSON输出:

This is my JSON output:

{
"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"键中的值,所以我尝试这样做

I need to get values stored in "mediaUrl" key so I tried to do

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"]会导致此错误:

but print j[key]["mediaUrl"] causes this error:

TypeError: list indices must be integers, not str

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

so I tried to do print j[key][0] but the result is not as I wanted to have (I want to have just mediaUrl value... btw j[key][1] causes list index out of range error):

{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'}

能给我一些建议吗?

推荐答案

这是应该做的列表理解

>>> [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']

工作原理

首先,您可以获取顶级键的列表

First you can get a list of the top-level keys

>>> 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'键的值

For each dictionary, grab the value for the 'mediaUrl' key

>>> [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']

这篇关于通过多级键解析JSON值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆