在Python标准库的json包中,提供了JSONEncoder和JSONDecoder两个类来实现Json字符串和dict类型数据的互相转换。
1.将Python dict类型转换成标准Json字符串
s={'a':'b','c':21}
d=json.JSONEncoder().encode(s)
print(type(d)) #str
print(d) #{"a":"b","c":21}
2.将json字符串转换成Python dict类型
json_str='{"a":"b","c":21}'
d=json.JSONDecoder().decode(json_str)
print(type(d)) #dict
print(d) #{'a':'b','c':21}