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.
该问题涉及将包含多个字典的列表转换为单一的JSON文档。使用Python的json库,可以使用json.dumps()函数实现此操作。同时,需要注意避免使用内置的list作为变量名以防止意外行为。
682

被折叠的 条评论
为什么被折叠?



