python - 如何正确设置此 API 调用的格式?

标签 python json telegram-bot python-telegram-bot

我正在制作一个电报聊天机器人,但不知道如何从输出中取出 [{'

def tether(bot, update):
    tetherCall = "https://api.omniexplorer.info/v1/property/31"
    tetherCallJson = requests.get(tetherCall).json()
    tetherOut = tetherCallJson ['issuances'][:1]
    update.message.reply_text("Last printed tether:  " + str (tetherOut)+" Please take TXID and past it in this block explorer to see more info: https://www.omniexplorer.info/search")

我的用户将看到此响应:[{'grant': '25000000.00000000', 'txid': 'f307bdf50d90c92278265cd92819c787070d6652ae3c8af46fa6a96278589b03'}]

最佳答案

这看起来像一个包含单个字典的列表:

[{'grant': '25000000.00000000',
  'txid': 'f307bdf50d90c92278265cd92819c787070d6652ae3c8af46fa6a96278589b03'}]

您应该能够通过使用 [0] 对列表进行索引来访问字典...

tetherOut[0]
# {'grant': '25000000.00000000',
#  'txid': 'f307bdf50d90c92278265cd92819c787070d6652ae3c8af46fa6a96278589b03'}

...如果您想从字典中获取特定值,您可以按其名称进行索引,例如

tetherOut[0]['txid']
# 'f307bdf50d90c92278265cd92819c787070d6652ae3c8af46fa6a96278589b03'

但是要小心链接这些东西。如果 tetherOut 是空列表,tetherOut[0] 将生成 IndexError。您可能想要捕获它(以及无效字典键将生成的 KeyError)。

关于python - 如何正确设置此 API 调用的格式?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52020056/

相关文章:

php - 通过 JSON、JQUERY、PHP 的图像预加载器

json - 将 3D 模型转换为 SceneJS JSON,包括纹理

bots - 我的 Telegram Bot 不断地发送消息

javascript - OpenAI API 404 响应

php - Telegram bot API 自定义键盘 - 如何制作垂直按钮列表?

Python numpy 根据另一个数组对数组进行排序并在轴上广播

python - textwrap 模块出现问题 : How to format a text file in paragraphs

json - 如何从 typescript 中的 json 获取串联键列表

python - 使用索引的函数聚合失败

python - 使用正则表达式查找段落中出现特定短语后的所有名词短语