1、dumps:将 python 中的 字典 转换为 字符串
import json
test_dict = {'bigberg': [7600, {1: [['iPhone', 6300], ['Bike', 800], ['shirt', 300]]}]}
print(test_dict)
print(type(test_dict))
#dumps 将数据转换成字符串
json_str = json.dumps(test_dict)
print(json_str)
print(type(json_str))
2、loads: 将 字符串 转换为 字典
new_dict = json.loads(json_str)
print(new_dict)
print(type(new_dict))
3、dump: 将数据写入 json 文件中
with open("../config/record.json","w") as f:
json.dump(new_dict,f)
print("加载入文件实现...")
4、load: 把文件关上,并把字符串变换为数据类型
with open("../config/record.json",'r') as load_f:
load_dict = json.load(load_f)
print(load_dict)
load_dict['smallberg'] = [8200,{1:[['Python',81],['shirt',300]]}]
print(load_dict)
with open("../config/record.json","w") as dump_f:
json.dump(load_dict,dump_f)
5、遍历 key value
test_json = {"a":1,"b":2,"c":3}
for key,value in test_json.items():
print key,value