如何为使用json的HTTP服务创建API包装器?

2024-04-28 16:52:50 发布

您现在位置:Python中文网/ 问答频道 /正文

我想做一个HTTP包装抽搐.tv在python中。我该怎么做?我知道如何使用httpget,仅此而已。我希望它像这样工作:

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

等等

我该怎么做这个包装纸呢?你知道吗


Tags: theimportgamehttpchatchannellinktv
1条回答
网友
1楼 · 发布于 2024-04-28 16:52:50

可以使用标准库的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方法来完成其余的工作。你知道吗

相关问题 更多 >