在Json中转换dctionary循环python
我正在为一个新的电报机器人编程。我已经创建了一个循环,它将变量在Json中转换dctionary循环python,python,json,loops,dictionary,telegram,Python,Json,Loops,Dictionary,Telegram,我正在为一个新的电报机器人编程。我已经创建了一个循环,它将变量I插入字典Idic,描述了一个新按钮。但是当我在Json字典中转换dic时,它给了我这个问题 import botogram import json bot = botogram.create("1210935912:AAG4X8vHlXLM3jQWnxFKDB2NsZ6pqTQM7lQ") list = ["La","Alaska","New Delhi"] bot.about = "Benvenuti" @bot.command
I
插入字典Idic
,描述了一个新按钮。但是当我在Json字典中转换dic
时,它给了我这个问题
import botogram
import json
bot = botogram.create("1210935912:AAG4X8vHlXLM3jQWnxFKDB2NsZ6pqTQM7lQ")
list = ["La","Alaska","New Delhi"]
bot.about = "Benvenuti"
@bot.command("start")
def start_command(chat, message):
for i in list:
dict = {
'chat_id': chat.id,
'text': 'Where are you?',
'reply_markup': {
'keyboard': [[
{'text': i},
{'text': 'Action B'},
],
[
{
'text': 'Use geolocation',
'request_location': True,
},
],
],
'resize_keyboard': True,
'one_time_keyboard': True,
'selective': True,
}
}
bot.api.call('sendMessage', json.loads(dict))
if __name__ == "__main__":
bot.run()
输出:
TypeError: the JSON object must be str, bytes or bytearray, not dict
json是数据结构的文本表示
json.load
将json字符串转换为字典。因为您已经有了一个字典,并且想要一个json字符串,所以应该使用json.dumps
方法。您需要使用json.dumps
将对象转换为json
bot.api.call('sendMessage', json.dumps(dict))
只需使用json.dumps(dict)。它不起作用:请求失败,代码为400。电报回复:“错误请求:消息文本为空”