Python 如何为使用json的HTTP服务创建API包装器?
我想用python为twitch.tv制作一个HTTP包装器。我该怎么做?我知道如何使用HTTPGET,仅此而已。我希望它能像这样工作:Python 如何为使用json的HTTP服务创建API包装器?,python,Python,我想用python为twitch.tv制作一个HTTP包装器。我该怎么做?我知道如何使用HTTPGET,仅此而已。我希望它能像这样工作: import twichtvwrapper twich = twichtvwrapper(useragent, username, password). channel = twich.channel(channelname) 然后所有json属性都会放在这里,如: print(channel.game) #this would say the last
import twichtvwrapper
twich = twichtvwrapper(useragent, username, password).
channel = twich.channel(channelname)
然后所有json属性都会放在这里,如:
print(channel.game) #this would say the last played game
print(channel.displayname) #this would print the display name of the channel
print(cahnnel.link.chat) #this will return the chat link
等等
如何制作这个包装器?您可以使用标准库的
JSON
模块在Python dict和JSON字符串之间进行转换
import json
# package a python dict as json
dict0 = {'spam': 'data', 'eggs': 'more data'}
pack = json.dumps(dict0)
# turn it back to a dict
dict1 = json.loads(pack)
>>> print dict1['spam']
data
因此,如果您的程序从HTTP请求获得JSON响应,只需将其转换为dict,然后使用常规的Python dict方法来完成其余操作。当我尝试运行您的代码时,它不起作用?!我使用的是Python2.7。奇怪的是“json.dump”会同时做这两件事。顺便说一句,你的android ide什么时候发布?我想在eink显示器(NST)上运行它,那么你能让它与android 2.1兼容吗?我只是整理一下标准输出。我应该在几周内准备好一些非常有用的东西。我会把它放到GitHub上,然后继续努力。它应该与Android2.1一起工作,但我还没有测试过它。