python之json方法load、loads、dump、dumps

一、json.load

import json
with open("json.json","r") as f:
    result = json.load(f) #从文件中读取json字符串然后转换成python对象
    print(result) #输出{"name":"test"}


二、json.loads

import json
str = '{"name":"test"}'
print(type(json.loads(str))) #将字符串转换成python对象,输出<class 'dict'>


三、json.dumps

import json
dist = {"name":"test"}
print(type(json.dumps(dist))) #将字典类型转换成字符串类型,输出<class 'str'>

四、json.dump

import json
dist = {"name":"test"}
with open("json.json","r") as f:
    json.dump(dist,f) #将python字典格式转换成字符串写入到文件中
    
    
posted @ 2020-12-28 21:06  亨利其实很坏  阅读(559)  评论(0)    收藏  举报