当前位置:首页>开发>正文

如何把python里面的list变成json对象

2023-09-08 08:46:02 互联网 未知 开发

如何把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"

}

最新文章