python的json使用方法与file.read()的差异

REPLACEMENTS = {"name":"kai"}#python字典类型对象
jso = json.dumps(REPLACEMENTS)#将字典对象转化为json对象,其实是字符串
print(type(jso))#输出str
dico = json.loads(jso)#将json对象转化为python字典对象
print(dico)#输出dict
with open("test.json","w") as f:
    f.write(jso)#注意写入一定是字符串类型

with open("test.json","r") as f1:
#    print(type(json.load(f1)))
    print(type(f1.read()))#文件对象输出是字符串类型
    
with open("test.json","r") as f1:
    print(type(json.load(f1)))
#    print(type(f1.read()))#文件对象输出为字典类型

输出
#json.dumps将字典数据转化为字符串
{‘name’: ‘kai’}#

你可能感兴趣的:(python的json使用方法与file.read()的差异)