Python 使用discord.py显示json中的数据
我目前正在制作一个显示osu的discord机器人!统计数据。这是我的代码:Python 使用discord.py显示json中的数据,python,json,python-3.x,bots,Python,Json,Python 3.x,Bots,我目前正在制作一个显示osu的discord机器人!统计数据。这是我的代码: @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 H
@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中给出了这一点:
[{
"user_id" : "1",
"username" : "User name",
"count300" : "1337", // Total amount for all ranked and approved beatmaps played
"count100" : "123", // Total amount for all ranked and approved beatmaps played
"count50" : "69", // Total amount for all ranked and approved beatmaps played
"playcount" : "42", // Only counts ranked and approved beatmaps
"ranked_score" : "666666", // Counts the best individual score on each ranked and approved beatmaps
"total_score" : "999999998", // Counts every score on ranked and approved beatmaps
"pp_rank" : "2442",
"level" : "50.5050",
"pp_raw" : "3113",
"accuracy" : "98.1234",
"count_rank_ss": "54",
"count_rank_s" : "81", // Counts for SS/S/A ranks on maps
"count_rank_a" : "862",
"country" : "DE", // Uses the ISO3166-1 alpha-2 country code naming. See this for more information: http://en.wikipedia.org/wiki/ISO_3166-1_alpha-2/wiki/ISO_3166-1_alpha-2)
"pp_country_rank":"1337", // The user's rank in the country.
"events" : [{ // Contains events for this user
"display_html" : "<img src='\/images\/A_small.png'\/>...",
"beatmap_id" : "222342",
"beatmapset_id" : "54851",
"date" : "2013-07-07 22:34:04",
"epicfactor" : "1" // How "epic" this event is (between 1 and 32)
}, { ... }, ...]
}]
[{
“用户id”:“1”,
“用户名”:“用户名”,
“count300”:“1337”,//播放的所有排名和批准的beatmaps的总金额
“count100”:“123”,//播放的所有排名和批准的beatmaps的总金额
“count50”:“69”,//播放的所有排名和批准的beatmaps的总金额
“播放次数”:“42”,//仅统计排名和批准的beatmaps
“排名分数”:“666666”//统计每个排名和批准的beatmaps上的最佳个人分数
“total_score”:“999999998”//统计排名和批准的beatmaps上的每个分数
“pp_排名”:“2442”,
“等级”:“50.5050”,
“原汁原味”:“3113”,
“准确度”:“98.1234”,
“计数等级”:“54”,
“count_rank_s”:“81”//地图上SS/s/A等级的计数
“计数等级a”:“862”,
“国家”:“DE”,//使用ISO3166-1 alpha-2国家代码命名。有关更多信息,请参阅此:http://en.wikipedia.org/wiki/ISO_3166-1_alpha-2/wiki/ISO_3166-1_alpha-2)
“pp_country_rank”:“1337”//用户在国家中的排名。
“事件”:[{//包含此用户的事件
“显示html”:“…”,
“beatmap_id”:“222342”,
“beatmapset_id”:“54851”,
“日期”:“2013-07-07 22:34:04”,
“epicfactor”:“1”//此事件的“epic”程度(介于1和32之间)
}, { ... }, ...]
}]
我的代码它在discord中显示如下
我只想展示json中的一些东西,比如“pp_raw”、“level”、“accurity”,等等。您可以使用python的内置
json
模块将json响应转换为python字典-只需导入json
,然后执行类似操作来访问响应的各个元素:数据=json.loads(response.json())[0]
。然后,您可以访问各个元素,如数据['pp_raw']
,并根据需要显示它们。数据=响应.json()返回字典对象的列表。因此,在访问值之前,必须首先指定索引。假设您要访问第一个对象中的pp_raw
,它将如下所示:
print data[0]['pp_raw']
好的,所以只打印JSON的一部分。你可以像访问任何其他字典一样访问它。这不太准确。你能给我一个片段吗?我刚刚试过,但它不起作用。当我添加
data=json.loads(response.json())wait bot.say(data['pp_raw'])
对不起,我没有看到响应是一个列表。现在编辑。