【问题标题】:Why is this Python dictionary (Json) returning a syntax error? [closed]为什么这个 Python 字典 (Json) 返回语法错误? [关闭]
【发布时间】:2020-06-26 08:48:08
【问题描述】:

我正在使用 Hangouts Chat API 制作机器人,但由于某种原因,我格式化的卡片让我遇到了语法错误。这是带有字典的变量:

bot_message = {
        "hangouts": {
            "header": {
                "title": article["title"],
                "subtitle": article["author"],
                "imageUrl": artile["urlToImage"]
            },
            {
                "widgets" [{
                    "buttons": [{
                        "textButton": {
                            "text": "Open this article on " + article["source"]["name"],
                            "onClick": {
                                "openLink": {
                                    "url": article["url"]
                                }
                            }
                        }
                    }]
                }]
            }
        }        # error occurs here
    }

基本上,我收到一个语法错误,说注释的大括号是无效的语法。有什么不匹配的吗?不应该有任何不匹配的东西,因为 VS Code(我现在正在使用的)会自动添加右括号。当错误在其他地方时,Python 是否有时会返回随机错误(就像在 Swift 中一样)?

有人能指出错误吗?

【问题讨论】:

  • 你在"imageUrl": artile["urlToImage"]这一行中也有一个错字(我认为)——我很确定应该是article

标签: python json google-chat


【解决方案1】:

"widgets" 后面需要一个冒号。

包含"widgets" 的对象是一个没有字典键的值,这是没有意义的。结构如下:

bot_message = {"hangouts": {...}, {"widgets": [...]}}

必须是:

bot_message = {"hangouts": {...}, "something": {"widgets": [...]}}

或:

bot_message = {"hangouts": {...}, "widgets": [...]}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多