簡體   English   中英

如何為使用json的HTTP服務制作API包裝?

[英]How can i make an API wrapper for a HTTP service that uses json?

我想在python中為twitch.tv創建HTTP包裝器。 我該怎么做? 我知道如何使用HTTP GET,僅此而已。 我希望它像這樣工作:

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字典和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方法來完成其余的工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM