Python:将字典列表转换为json [英] Python: converting a list of dictionaries to json
问题描述
list = [{'id':123, 'data':'qwerty','indices':[1,10]},{'id':345,'data':'mnbvc','indices':[2,11]}]
等等。列表中可能会有更多的文档。我需要将这些转换成一个JSON文档,可以通过瓶子返回,我不明白如何做到这一点。请帮忙。我在这个网站上看到类似的问题,但我无法理解那里的解决方案。
使用json库
import json
json.dumps(list)
顺便说一下,您可能会考虑将变量列表更改为另一个名称,列表
是列表创建的内置函数,您可以如果您不更改变量名称,可能会收到一些意想不到的行为或一些错误代码。
I have a list of dictionaries, looking some thing like this:
list = [{'id': 123, 'data': 'qwerty', 'indices': [1,10]}, {'id': 345, 'data': 'mnbvc', 'indices': [2,11]}]
and so on. There may be more documents in the list. I need to convert these to one JSON document, that can be returned via bottle, and I cannot understand how to do this. Please help. I saw similar questions on this website, but I couldn't understand the solutions there.
use json library
import json
json.dumps(list)
by the way, you might consider changing variable list to another name, list
is the builtin function for a list creation, you may get some unexpected behaviours or some buggy code if you don't change the variable name.
这篇关于Python:将字典列表转换为json的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!