json库dumps和loads方法使用

# -*- coding:utf-8 -*-

import json


content = "[\"[\\\"asset-8nmfb4hb\\\",0,0,\\\"2022-04-28 15:57:00.000\\\"]\",\"[\\\"asset-8nmfb4hb\\\",0,0,\\\"2022-04-28 15:56:30.000\\\"]\",\"[\\\"asset-8nmfb4hb\\\",0,1,\\\"2022-04-28 15:56:00.000\\\"]\",\"[\\\"asset-8nmfb4hb\\\",0,0.3333333,\\\"2022-04-28 15:55:30.000\\\"]\"]"

data = {"datas": '[{"name": "菜鸟"}]'}

# 数据转字符串
res = json.dumps(data, ensure_ascii=False)
#print(res)
"""
{"datas": "[{\"name\": \"菜鸟\"}]"}
"""

# 去掉转义符
dd = eval(res)
#print(dd)
"""
{'datas': '[{"name": "\xe8\x8f\x9c\xe9\xb8\x9f"}]'}
"""

# 字符串序列化
dic = json.loads(res)
print(type(dic))
"""
{u'datas': u'[{"name": "\u83dc\u9e1f"}]'}
"""

list = json.loads(content)
print(type(list))
print(list[0])
"""
[u'["asset-8nmfb4hb",0,0,"2022-04-28 15:57:00.000"]', u'["asset-8nmfb4hb",0,0,"2022-04-28 15:56:30.000"]', u'["asset-8nmfb4hb",0,1,"2022-04-28 15:56:00.000"]', u'["asset-8nmfb4hb",0,0.3333333,"2022-04-28 15:55:30.000"]']
"""

pp = json.dumps(list)
print(pp)

  

posted @ 2022-05-06 16:19  ReluStarry  阅读(73)  评论(0编辑  收藏  举报