json格式与字符串格式相互转换:
序列化 json.loads()
反序列化 # json.dumps(obj,ensure_ascii=True,indent=None,sort_keys=False)
import json
# json_str='{"name":"Felix","age":18}'
# print(json_str,type(json_str))
# load_data=json.loads(json_str)
# print(load_data,type(load_data))
# with open('felix.json','r',encoding='utf-8') as f:
# load_data=json.load(f)
# print(load_data,type(load_data))
#
# json.dumps(obj,ensure_ascii=True,indent=None,sort_keys=False)
data={
"name":"Felix",
"age":18,
"hobby": ['运动','妹子'],
"friends": [
{
"name":"刘德华"
},
{
"name":"梁朝伟"
}
]
}
json_str=json.dumps(data)
print(json_str)
print(json.dumps(data,ensure_ascii=False))
print(json.dumps(data,ensure_ascii=False,indent=4))
print(json.dumps(data,ensure_ascii=False,indent=4,sort_keys=True))
网友评论