解析JSON数据Python [英] Parsing JSON data Python

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

问题描述

所以我试图获取用户在Twitch上关注的频道列表,

So i'm trying to fetch a list of channels that a user follows on Twitch,

我正在使用urllib2从以下位置获取数据:

I'm using urllib2 to fetch the data from:

http://api.twitch. tv/kraken/users/user/follows/channels?offset = 0& on_site = 1

但是我只想要用户遵循的频道列表,这是原始json数据的样子,我只想要每个频道的display_name,例如['Syndicate','Gem28daz2012'],我该怎么做?

however I only want a list of channels that the user follows, here is what the raw json data looks like, I only want the display_name from each channel e.g ['Syndicate','Gem28daz2012'], how could I do this?

{
    "follows": [{
        "created_at": "2016-01-15T22:43:35Z",
        "_links": {
            "self": "https://api.twitch.tv/kraken/users/user/follows/channels/syndicate"
        },
        "notifications": false,
        "channel": {
            "_links": {
                "self": "https://api.twitch.tv/kraken/channels/syndicate",
                "follows": "https://api.twitch.tv/kraken/channels/syndicate/follows",
                "commercial": "https://api.twitch.tv/kraken/channels/syndicate/commercial",
                "stream_key": "https://api.twitch.tv/kraken/channels/syndicate/stream_key",
                "chat": "https://api.twitch.tv/kraken/chat/syndicate",
                "features": "https://api.twitch.tv/kraken/channels/syndicate/features",
                "subscriptions": "https://api.twitch.tv/kraken/channels/syndicate/subscriptions",
                "editors": "https://api.twitch.tv/kraken/channels/syndicate/editors",
                "videos": "https://api.twitch.tv/kraken/channels/syndicate/videos",
                "teams": "https://api.twitch.tv/kraken/channels/syndicate/teams"
            },
            "background": null,
            "banner": null,
            "broadcaster_language": "en",
            "display_name": "Syndicate",
            "game": "Counter-Strike: Global Offensive",
            "logo": "https://static-cdn.jtvnw.net/jtv_user_pictures/syndicate-profile_image-03bf1d6ad0025f86-300x300.png",
            "mature": false,
            "status": "Road To MLG! (CS:GO Competiive) ",
            "partner": true,
            "url": "http://www.twitch.tv/syndicate",
            "video_banner": "https://static-cdn.jtvnw.net/jtv_user_pictures/syndicate-channel_offline_image-a15fc19d9828b9c7-640x360.png",
            "_id": 16764225,
            "name": "syndicate",
            "created_at": "2010-10-17T23:19:43Z",
            "updated_at": "2016-01-22T22:17:33Z",
            "delay": null,
            "followers": 2353424,
            "profile_banner": "https://static-cdn.jtvnw.net/jtv_user_pictures/syndicate-profile_banner-5f912a83f7e9bafe-480.png",
            "profile_banner_background_color": null,
            "views": 38140282,
            "language": "en"
        }
    }, {
        "created_at": "2016-01-12T21:01:49Z",
        "_links": {
            "self": "https://api.twitch.tv/kraken/users/user/follows/channels/gem28daz2012"
        },
        "notifications": false,
        "channel": {
            "_links": {
                "self": "http://api.twitch.tv/kraken/channels/gem28daz2012",
                "follows": "http://api.twitch.tv/kraken/channels/gem28daz2012/follows",
                "commercial": "http://api.twitch.tv/kraken/channels/gem28daz2012/commercial",
                "stream_key": "http://api.twitch.tv/kraken/channels/gem28daz2012/stream_key",
                "chat": "http://api.twitch.tv/kraken/chat/gem28daz2012",
                "features": "http://api.twitch.tv/kraken/channels/gem28daz2012/features",
                "subscriptions": "http://api.twitch.tv/kraken/channels/gem28daz2012/subscriptions",
                "editors": "http://api.twitch.tv/kraken/channels/gem28daz2012/editors",
                "videos": "http://api.twitch.tv/kraken/channels/gem28daz2012/videos",
                "teams": "http://api.twitch.tv/kraken/channels/gem28daz2012/teams"
            },
            "background": null,
            "banner": null,
            "broadcaster_language": null,
            "display_name": "Gem28daz2012",
            "game": "Minecraft",
            "logo": null,
            "mature": null,
            "status": "Playing minecraft",
            "partner": false,
            "url": "http://www.twitch.tv/gem28daz2012",
            "video_banner": null,
            "_id": 93338000,
            "name": "gem28daz2012",
            "created_at": "2015-06-12T19:51:58Z",
            "updated_at": "2016-01-12T21:16:07Z",
            "delay": null,
            "followers": 8,
            "profile_banner": null,
            "profile_banner_background_color": null,
            "views": 4,
            "language": "en"
        }
    }],
"_total": 2,
"_links": {
    "self": "https://api.twitch.tv/kraken/users/user/follows/channels?direction=DESC&limit=25&offset=0&sortby=created_at",
    "next": "https://api.twitch.tv/kraken/users/user/follows/channels?direction=DESC&limit=25&offset=25&sortby=created_at"
}

推荐答案

要解析http响应正文,可以使用

To parse the http response body, you can use json.loads. After that you can use a list comprehesion:

display_names = [f['channel']['display_name'] for f in data['follows']]

在某些情况下使用它:

data_str = '''

{
    "follows": [{
        "created_at": "2016-01-15T22:43:35Z",
        "_links": {
            "self": "https://api.twitch.tv/kraken/users/user/follows/channels/syndicate"
        },
        "notifications": false,
        "channel": {
            "_links": {
                "self": "https://api.twitch.tv/kraken/channels/syndicate",
                "follows": "https://api.twitch.tv/kraken/channels/syndicate/follows",
                "commercial": "https://api.twitch.tv/kraken/channels/syndicate/commercial",
                "stream_key": "https://api.twitch.tv/kraken/channels/syndicate/stream_key",
                "chat": "https://api.twitch.tv/kraken/chat/syndicate",
                "features": "https://api.twitch.tv/kraken/channels/syndicate/features",
                "subscriptions": "https://api.twitch.tv/kraken/channels/syndicate/subscriptions",
                "editors": "https://api.twitch.tv/kraken/channels/syndicate/editors",
                "videos": "https://api.twitch.tv/kraken/channels/syndicate/videos",
                "teams": "https://api.twitch.tv/kraken/channels/syndicate/teams"
            },
            "background": null,
            "banner": null,
            "broadcaster_language": "en",
            "display_name": "Syndicate",
            "game": "Counter-Strike: Global Offensive",
            "logo": "https://static-cdn.jtvnw.net/jtv_user_pictures/syndicate-profile_image-03bf1d6ad0025f86-300x300.png",
            "mature": false,
            "status": "Road To MLG! (CS:GO Competiive) ",
            "partner": true,
            "url": "http://www.twitch.tv/syndicate",
            "video_banner": "https://static-cdn.jtvnw.net/jtv_user_pictures/syndicate-channel_offline_image-a15fc19d9828b9c7-640x360.png",
            "_id": 16764225,
            "name": "syndicate",
            "created_at": "2010-10-17T23:19:43Z",
            "updated_at": "2016-01-22T22:17:33Z",
            "delay": null,
            "followers": 2353424,
            "profile_banner": "https://static-cdn.jtvnw.net/jtv_user_pictures/syndicate-profile_banner-5f912a83f7e9bafe-480.png",
            "profile_banner_background_color": null,
            "views": 38140282,
            "language": "en"
        }
    }, {
        "created_at": "2016-01-12T21:01:49Z",
        "_links": {
            "self": "https://api.twitch.tv/kraken/users/pandaswede/follows/channels/gem28daz2012"
        },
        "notifications": false,
        "channel": {
            "_links": {
                "self": "http://api.twitch.tv/kraken/channels/gem28daz2012",
                "follows": "http://api.twitch.tv/kraken/channels/gem28daz2012/follows",
                "commercial": "http://api.twitch.tv/kraken/channels/gem28daz2012/commercial",
                "stream_key": "http://api.twitch.tv/kraken/channels/gem28daz2012/stream_key",
                "chat": "http://api.twitch.tv/kraken/chat/gem28daz2012",
                "features": "http://api.twitch.tv/kraken/channels/gem28daz2012/features",
                "subscriptions": "http://api.twitch.tv/kraken/channels/gem28daz2012/subscriptions",
                "editors": "http://api.twitch.tv/kraken/channels/gem28daz2012/editors",
                "videos": "http://api.twitch.tv/kraken/channels/gem28daz2012/videos",
                "teams": "http://api.twitch.tv/kraken/channels/gem28daz2012/teams"
            },
            "background": null,
            "banner": null,
            "broadcaster_language": null,
            "display_name": "Gem28daz2012",
            "game": "Minecraft",
            "logo": null,
            "mature": null,
            "status": "Playing minecraft",
            "partner": false,
            "url": "http://www.twitch.tv/gem28daz2012",
            "video_banner": null,
            "_id": 93338000,
            "name": "gem28daz2012",
            "created_at": "2015-06-12T19:51:58Z",
            "updated_at": "2016-01-12T21:16:07Z",
            "delay": null,
            "followers": 8,
            "profile_banner": null,
            "profile_banner_background_color": null,
            "views": 4,
            "language": "en"
        }
    }],
"_total": 2,
"_links": {
    "self": "https://api.twitch.tv/kraken/users/pandaswede/follows/channels?direction=DESC&limit=25&offset=0&sortby=created_at",
    "next": "https://api.twitch.tv/kraken/users/pandaswede/follows/channels?direction=DESC&limit=25&offset=25&sortby=created_at"
}
}

'''

import json
data = json.loads(data_str)
display_names = [f['channel']['display_name'] for f in data['follows']]
print(display_names) # ['Syndicate', 'Gem28daz2012']

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

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