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

Posted

技术标签:

【中文标题】在 api 响应中创建多个特定于数字的值【英文标题】:Create a number of values specific to a number in an api response 【发布时间】:2019-03-28 10:52:48 【问题描述】:

所以我需要为不和谐嵌入设置与 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: 数字设置它的原因是因为它们的数量经常变化,但是我不知道该怎么做......这是我的带有 2 个值的代码来显示我的想法:

    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!')

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

【问题讨论】:

【参考方案1】:

看起来所有这些项目都可以在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!')

【讨论】:

抱歉回复晚了,不知道您是否会看到这个,但返回时出现错误代码集“Failed to get API data for unreleased items, there might be none! 没关系必须摆弄 api 请求让它工作谢谢!

以上是关于在 api 响应中创建多个特定于数字的值的主要内容,如果未能解决你的问题,请参考以下文章

如何使用mockito在安卓系统中创建模拟api响应[关闭]。

如何在谷歌地图api中创建多个信息窗口

在 Facebook Marketing api 中创建 geo_locations 定位规范时获取无效的参数响应

使用 cURL 在 BIM 360 中创建问题(无响应)

如何在 C# Asp.Net 中从相当复杂的 Json 响应中创建 Dto

使用 REST API 处理多响应数据