字典转json
1 #/usr/bin/env python3 2 import json 3 s = {'k1':'v1','k3':{'ke':'a'}} 4 json_str = json.dumps(s) 5 print (json_str) 6 print (type(json_str)) 7 8 {"k1": "v1", "k3": {"ke": "a"}} 9 <class 'str'>
json 转 dict
1 import json 2 s = '{"k3": {"ke": "a"}, "k1": "v1"}' # json格式的数据在python里是string类型。 3 json_str = json.loads(s) 4 print (json_str) 5 print (type(json_str)) 6 7 {'k1': 'v1', 'k3': {'ke': 'a'}} 8 <class 'dict'>
解决 json dumps中文字符乱码问题。
json默认使用ascii编码,我们禁用它就可以正常显示中文了
event_json = json.dumps(event, ensure_ascii=False)