Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/352.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 向JSON响应动态添加数据_Python_Json - Fatal编程技术网

Python 向JSON响应动态添加数据

Python 向JSON响应动态添加数据,python,json,Python,Json,我有一个RESTful API,它以JSON格式返回数据库表的所有元素。我需要在集合+json中显示各个链接/ID @app.route('/table/showall/<table>', methods = ['GET']) def api_showAll(table) -> str: if request.method == 'GET': with DBcm.UseDatabase(DBconfig) as cursor:

我有一个RESTful API,它以JSON格式返回数据库表的所有元素。我需要在集合+json中显示各个链接/ID

@app.route('/table/showall/<table>', methods = ['GET'])
def api_showAll(table) -> str:
    if request.method == 'GET':
        with DBcm.UseDatabase(DBconfig) as cursor:

            _SQLlist = "SELECT * FROM %s;" % table
            cursor.execute(_SQLlist)
            data = cursor.fetchall()


Links = { "collection" :
            {
                "hrefs" : 
                    [
                        { "href": "127.0.0.1:5000/table/showone/tableNam/id"},
                        { "href": "127.0.0.1:5000/table/showone/tableNam/id"},
                        { "href": "127.0.0.1:5000/table/showone/tableName/id"}
                    ]
                }
    }

return json.dumps(str(Links))
@app.route('/table/showall/',方法=['GET'])
def api_showAll(表)->str:
如果request.method==“GET”:
使用DBcm.UseDatabase(DBconfig)作为游标:
_SQLlist=“从%s中选择*;%table
cursor.execute(_SQLlist)
data=cursor.fetchall()
链接={“集合”:
{
“人权高专办”:
[
{“href”:“127.0.0.1:5000/table/showone/tableNam/id”},
{“href”:“127.0.0.1:5000/table/showone/tableNam/id”},
{“href”:“127.0.0.1:5000/table/showone/tableName/id”}
]
}
}
返回json.dumps(str(Links))
由于插入,数据库中的数据将随着时间的推移而增长,我如何将单个项目动态添加到JSON中以显示在屏幕上?上面你可以看到我到目前为止所做的尝试。它显示了3个硬编码的值,但我需要动态执行


如果您需要任何帮助

创建一个类来复制您的模型并使其可
json序列化
,然后您可以
json.dumps(object)
,您可以在这里找到一个可能的解决方案代码