显示来自json的数据不和.py

2024-06-06 15:05:26 发布

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

我目前正在制作一个不和谐机器人,将显示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}$

我的代码在不和谐中显示它

discord screenshot

我只想展示json中的一些东西,比如“ppunaw”、“level”、“accuracity”等等


Tags: the代码nameapijsonurldataget
2条回答

您可以使用python的内置json模块将json响应转换为python字典—只需import json,然后执行类似的操作来访问响应的各个元素:data = json.loads(response.json())[0]。然后您可以访问单个元素,如data['pp_raw'],并根据需要显示它们。在

data = response.json()返回字典对象的列表。所以在访问值之前,必须先指定索引。假设您要访问第一个对象中的pp_raw,它看起来像:

print data[0]['pp_raw']

相关问题 更多 >