我目前正在制作一个不和谐机器人,将显示osu!统计数据。这是我的代码:
@bot.command()
async def osu(osu_name : str):
"""Adds two numbers together."""
await bot.say("Fetching data")
url = 'https://osu.ppy.sh/api/get_user?k={ my api key }&u=' + osu_name
# Do the HTTP get request
response = requests.get(url, verify=True) #Verify is check SSL certificate
# Decode the JSON response into a dictionary and use the data
await bot.say(response.json())
奥苏!api在其json中提供了以下内容:
^{pr2}$我的代码在不和谐中显示它
我只想展示json中的一些东西,比如“ppunaw”、“level”、“accuracity”等等
您可以使用python的内置
json
模块将json响应转换为python字典—只需import json
,然后执行类似的操作来访问响应的各个元素:data = json.loads(response.json())[0]
。然后您可以访问单个元素,如data['pp_raw']
,并根据需要显示它们。在data = response.json()
返回字典对象的列表。所以在访问值之前,必须先指定索引。假设您要访问第一个对象中的pp_raw
,它看起来像:相关问题 更多 >
编程相关推荐