dict与json相互转换之前要导包import json
import json
d_json = '{"B": 2, "a": 1}'
d_dict = json.loads(d_json)
d_dict['c'] = 101
d_json2 = json.dumps(d_dict)
print(d_dict) # {'B': 2, 'a': 1, 'c': 101}
print(d_json2) # {"B": 2, "a": 1, "c": 101}
import json
d_json1 = '{"a":"ready"}'
print(d_json1) # {"a":"ready"}
print(type(d_json1)) # <class 'str'>
d_dict1 = json.loads(d_json1)
print(d_dict1) # {'a': 'ready'}
print(type(d_dict1)) # <class 'dict'>
小结:
json一般是类似于dict的字符串格式才能转换成 dict
在json里,只要是字符串,一定要使用双引号" "
print时候,json和dict有单双引号区别