python - 创建特定于 api 响应中的数字的多个值

标签 python json discord discord.py

因此,我需要设置与 API 响应内的数字相对应的多个值,以实现不和谐嵌入:

    {rows: 7, vbucks: "https://fortnite-public-files.theapinetwork.com/fortnite-vbucks-icon.png",…}
items: [{itemid: "d17ae35-1b64bca-f0cebef-e097b12", name: "Divine Dragon", cost: "???", item: {,…}},…]
0: {itemid: "d17ae35-1b64bca-f0cebef-e097b12", name: "Divine Dragon", cost: "???", item: {,…}}
1: {itemid: "e072f22-591edf6-1243f3d-1686821", name: "Guan Yu", cost: "???", item: {,…}}
2: {itemid: "51c5fcc-445f634-eacbd39-540174d", name: "Loyal Shield", cost: "???", item: {,…}}
3: {itemid: "a7c3fc6-75503a3-393e6cd-8ee4c91", name: "Headbanger", cost: "???",…}
4: {itemid: "88b0141-ba800b9-030f4d4-3efac45", name: "Guandao", cost: "???", item: {,…}}
5: {itemid: "8f7d376-2c49df9-ad16235-6538f87", name: "Onesie", cost: "???", item: {,…}}
6: {itemid: "3c83cd1-592e881-d91e78b-34442d8", name: "Bullseye", cost: "???", item: {,…}}
rows: 7

看看它是怎么说的rows: 7 那里有 7 个东西?我需要为每个下拉数字设置 2 个值,我需要根据 rows: 数字设置它的原因是因为它们的数量经常变化,但是我不知道如何做到这一点...这是我的代码,其中有两个值来展示我的想法:

    if unreleased in ('unreleased','upcoming'):
  upcoming = fortnite_api_upcoming(unreleased)

  if upcoming:
    upcomingname0 = upcoming[0]['name']
    upcomingtype0 = upcoming[0]['item']['type']
    upcomingname1 = upcoming[1]['name']
    upcomingtype1 = upcoming[1]['item']['type']

    embed = discord.Embed(title="Item API Search Result", color=0xc600bc)

    embed.set_footer(text="by BattleDash#3866", icon_url="https://pbs.twimg.com/profile_images/1038570723382415361/wVhgKMug_400x400.jpg")
    embed.add_field(name="Unreleased Items", value='{}, **type:** {}\n'.format(upcomingname0, upcomingtype0), inline=False)
    embed.add_field(name="Unreleased Items", value='{}, **type:** {}\n'.format(upcomingname1, upcomingtype1), inline=False)
    await client.send_message(message.channel, embed=embed)
  else:
    await client.send_message(message.channel, 'Failed to get API data for unreleased items, there might be none!')

感谢任何可以提供帮助的人!

最佳答案

看起来所有这些项目都可以在 items 列表中找到。为什么不直接迭代它并使用其中的每个项目?

if upcoming:
    embed = discord.Embed(title="Item API Search Result", color=0xc600bc)
    embed.set_footer(text="by BattleDash#3866", icon_url="https://pbs.twimg.com/profile_images/1038570723382415361/wVhgKMug_400x400.jpg")

    for item in upcoming['items']:
        item_name = item['name']
        item_type = item['item']['type']
        embed.add_field(name="Unreleased Items", value='{}, **type:** {}\n'.format(item_name, item_type), inline=False)

    await client.send_message(message.channel, embed=embed)
else:
    await client.send_message(message.channel, 'Failed to get API data for unreleased items, there might be none!')

关于python - 创建特定于 api 响应中的数字的多个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52961953/

相关文章:

python - 在 kivy lang 和 python 中访问子部件属性的值

python - 禁用给定模块或目录的 pylint 消息

python - Django 装置 : Importing Nested Models

java - 按钮交互失败

javascript - Discord.js 如何为我的机器人所在的每个公会创建邀请?

python - discord.py 的颜色代码

python - 设置Flask+uWSGI+nginx的最佳实践

python - 如何检查Python中的列表列表中是否存在某个元素?

ios - 尝试在 Swift 中将有效的 JSON 数据从 NSURLConnection 转换为 NSDictonary 时发生崩溃/错误

python - 在 Python 中将原始推文字符串转换为 JSON 对象