如何在Flask中jsonify列表? [英] How do I `jsonify` a list in Flask?
本文介绍了如何在Flask中jsonify列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我知道可能存在安全原因
https://github.com/mitsuhiko/flask/issues/170> https://github.com/mitsuhiko/flask/issues/170 ,但我仍然想要一个方法返回一个如下所示的JSON列表: [
{'a':1,'b':2 },
{'a':5,'b':10}
]
而不是
{'results':[
{'a':1,'b': 2},
{'a':5,'b':10}
]}
响应 application / json
请求。如何使用Jsonify在Flask中返回JSON列表?
解决方案 出于安全原因,阻止您执行此。
无论如何,只要在Python标准库中使用 json.dumps
即可。
http://docs.python.org/library/ json.html#json.dumps
Currently Flask
would raise an error when jsonifying a list.
I know there could be security reasons https://github.com/mitsuhiko/flask/issues/170, but I still would like to have a way to return a JSON list like the following:
[
{'a': 1, 'b': 2},
{'a': 5, 'b': 10}
]
instead of
{ 'results': [
{'a': 1, 'b': 2},
{'a': 5, 'b': 10}
]}
on responding to a application/json
request. How can I return a JSON list in Flask using Jsonify?
解决方案
jsonify
prevents you from doing this for security reasons.
To do it anyway, just use json.dumps
in the Python standard library.
http://docs.python.org/library/json.html#json.dumps
这篇关于如何在Flask中jsonify列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文