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