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"
}

你可能感兴趣的:(Python中list转成Json字符串)