#json.dumps json.loads
import json
dict_name={"name":"yaoming","age":18}
dict_json_dump = json.dumps(dict_name)
with open("text.txt","w+",encoding="utf-8") as f:
f.write(dict_json_dump)
f.seek(0)
data_read=f.read()
print (data_read,type(data_read)) # {"name": "yaoming", "age": 18} <class 'str'>
f.seek(0)
data_json_read=json.loads(data_read)
print(data_json_read,type(data_json_read)) # {'name': 'yaoming', 'age': 18} <class 'dict'>
#json.dump json.load 用于文件的存储和读取 简化了文件的读取操作
dict_name = {"name": "yaoming", "age": 18}
with open("text.txt", "w+", encoding="utf-8") as f2:
json.dump(dict_name,f2) #直接序列化写入
f2.seek(0)
data_load_read = json.load(f2) #读取出来再还原原来存储的类型
print(data_load_read, type(data_load_read))
# {'name': 'yaoming', 'age': 18} <class 'dict'>