【问题标题】:Creating a dynamic JSON using a python list使用 python 列表创建动态 JSON
【发布时间】:2022-01-19 05:53:22
【问题描述】:

这是我在 python 文件中的 json 结构。这里的 stationList_of_state 是一个 python 列表,它有 5-10 个值,这些值会根据代码动态变化。

        message = {
        "type": "template",
        "payload": {
            "template_type": "generic",
            "elements": 
            [
                {
                    "buttons": [
                            {
                                "title": stationList_of_state[1],
                                "payload": stationList_of_state[1],
                                "type": "postback"
                            }
                        ]
                    }
                ]
            }
        }

我尝试过类似这样的方法,但显示错误:

        message = {
            "type": "template",
            "payload": {
                "template_type": "generic",
                "elements": 
                [
                for i in range(len(stationList_of_state)):
                    {
                        "buttons": [
                                {
                                    "title": stationList_of_state[i],
                                    "payload": stationList_of_state[i],
                                    "type": "postback"
                                }
                            ]
                        }
                    ]
                }
            }

有人可以建议我所做的替代方法吗?

【问题讨论】:

    标签: python json


    【解决方案1】:

    你快到了:

            message = {
                "type": "template",
                "payload": {
                    "template_type": "generic",
                    "elements": [
                        {
                            "buttons": [
                                {
                                    "title": stationList_of_state[i],
                                    "payload": stationList_of_state[i],
                                    "type": "postback",
                                }
                            ]
                        }
                        for i in range(len(stationList_of_state))
                    ],
                },
            }
    

    或者,简化for 子句以省略不必要的i 变量,

    
            message = {
                "type": "template",
                "payload": {
                    "template_type": "generic",
                    "elements": [
                        {
                            "buttons": [
                                {
                                    "title": station,
                                    "payload": station,
                                    "type": "postback",
                                }
                            ]
                        }
                        for station in stationList_of_state
                    ],
                },
            }
    

    【讨论】:

      猜你喜欢
      • 2016-05-10
      • 2021-04-02
      • 2018-10-14
      • 1970-01-01
      • 2021-08-04
      • 2018-06-04
      • 2020-05-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多