-1

我正在尝试在 python 中解码一个 twitch api 答案。

import urllib2
import json


url  = 'http://api.justin.tv/api/stream/list.json?channel=kungentv'

result =json.loads(urllib2.urlopen(url, timeout = 100).read().decode('utf-8'))
print result

如果我运行这个我得到这个:

[{u'broadcast_part': 6, u'featured': True, u'channel_subscription': True, u'embed_count': 0, u'id': u'9602378624', u'category': u'gaming', u'title': u'5x Legend - Playing the TOP DECKS from DECK WARS to Legendary! Handlock, Miracle and Shaman. (Never played Shaman EVUR)', u'video_height': 1080, u'site_count': 0, u'embed_enabled': False, u'channel': {u'category': u'gaming', u'status': u'5x Legend - Playing the TOP DECKS from DECK WARS to Legendary! Handlock, Miracle and Shaman. (Never played Shaman EVUR)', u'views_count': 56403101, u'subcategory': None, u'language': u'en', u'title': u'kungentv', u'screen_cap_url_huge': u'http://static-cdn.jtvnw.net/previews/live_user_kungentv-630x473.jpg', u'producer': True, u'tags': None, u'subcategory_title': u'', u'category_title': u'', u'screen_cap_url_large': u'http://static-cdn.jtvnw.net/previews/live_user_kungentv-320x240.jpg', u'mature': None, u'screen_cap_url_small': u'http://static-cdn.jtvnw.net/previews/live_user_kungentv-70x53.jpg', u'screen_cap_url_medium': u'http://static-cdn.jtvnw.net/previews/live_user_kungentv-150x113.jpg', u'timezone': u'Europe/Stockholm', u'login': u'kungentv', u'channel_url': u'http://www.justin.tv/kungentv', u'id': 30383713, u'meta_game': u'Hearthstone: Heroes of Warcraft'}, u'up_time': u'Mon May 19 00:34:43 2014', u'meta_game': u'Hearthstone: Heroes of Warcraft', u'format': u'live', u'stream_type': u'live', u'channel_count': 3671, u'abuse_reported': False, u'video_width': 1920, u'geo': u'SE', u'name': u'live_user_kungentv', u'language': u'en', u'stream_count': 0, u'video_bitrate': 3665.328125, u'broadcaster': u'obs', u'channel_view_count': 0}]

我如何对此进行解码,以便可以将其用作普通的字符串字典。

提前致谢!

4

1 回答 1

3

你不必解码任何东西;Python 将根据您的需要对字符串值进行编码和解码。

演示:

>>> result = [{u'broadcast_part': 6, u'featured': True, u'channel_subscription': True, u'embed_count': 0, u'id': u'9602378624', u'category': u'gaming', u'title': u'5x Legend - Playing the TOP DECKS from DECK WARS to Legendary! Handlock, Miracle and Shaman. (Never played Shaman EVUR)', u'video_height': 1080, u'site_count': 0, u'embed_enabled': False, u'channel': {u'category': u'gaming', u'status': u'5x Legend - Playing the TOP DECKS from DECK WARS to Legendary! Handlock, Miracle and Shaman. (Never played Shaman EVUR)', u'views_count': 56403101, u'subcategory': None, u'language': u'en', u'title': u'kungentv', u'screen_cap_url_huge': u'http://static-cdn.jtvnw.net/previews/live_user_kungentv-630x473.jpg', u'producer': True, u'tags': None, u'subcategory_title': u'', u'category_title': u'', u'screen_cap_url_large': u'http://static-cdn.jtvnw.net/previews/live_user_kungentv-320x240.jpg', u'mature': None, u'screen_cap_url_small': u'http://static-cdn.jtvnw.net/previews/live_user_kungentv-70x53.jpg', u'screen_cap_url_medium': u'http://static-cdn.jtvnw.net/previews/live_user_kungentv-150x113.jpg', u'timezone': u'Europe/Stockholm', u'login': u'kungentv', u'channel_url': u'http://www.justin.tv/kungentv', u'id': 30383713, u'meta_game': u'Hearthstone: Heroes of Warcraft'}, u'up_time': u'Mon May 19 00:34:43 2014', u'meta_game': u'Hearthstone: Heroes of Warcraft', u'format': u'live', u'stream_type': u'live', u'channel_count': 3671, u'abuse_reported': False, u'video_width': 1920, u'geo': u'SE', u'name': u'live_user_kungentv', u'language': u'en', u'stream_count': 0, u'video_bitrate': 3665.328125, u'broadcaster': u'obs', u'channel_view_count': 0}]
>>> result[0]['broadcast_part']
6

Python 2 根据需要使用 ASCII 在 Unicode 和字节字符串值之间进行编码/解码。

一般来说,您希望您的文本数据为 Unicode 值;你的程序应该是一个 Unicode 三明治。收到数据时将其解码为 Unicode,再次发送时进行编码。它就像任何其他序列化数据一样;当您可以使用datetime对象时,您不使用字符串时间戳,如果您尝试进行数字计算,则不使用字节字符串,您将转换为intor floatordecimal.Decimal()值等等。

于 2014-05-19T10:34:24.183 回答