首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在Python中正确地解析这个JSON数据?

如何在Python中正确地解析这个JSON数据?
EN

Stack Overflow用户
提问于 2016-01-22 23:18:16
回答 1查看 426关注 0票数 0

所以我试图获取一个用户在Twitch上跟踪的频道列表,

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

site=1

然而,我只想要一个用户遵循的频道列表,以下是原始json数据的样子,我只想从每个频道获得display_name,比如'Syndicate','Gem28daz2012',我怎么能做到这一点?

代码语言:javascript
复制
{
    "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"
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-01-22 23:21:30

要解析http响应体,可以使用json.loads。在此之后,您可以使用列表比较:

代码语言:javascript
复制
display_names = [f['channel']['display_name'] for f in data['follows']]

在某些情况下可以这样说:

代码语言:javascript
复制
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']
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34957557

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档