如何把python里面的list变成json对象
如何把python里面的list变成json对象?
代码示例:
# list 转成Json格式数据
def listToJson(lst):
import json
import numpy as np
keys = [str(x) for x in np.arange(len(lst))]
list_json = dict(zip(keys, lst))
str_json = json.dumps(list_json, indent=2, ensure_ascii=False) # json转为string
return str_json
结果
{
"0": "123",
"2": "34",
"3": "456",
"1": "345"
}