一、对json文件的读取
data1={"programmers":[{ "firstName": "Brett", "lastName":"McLaughlin", "email": "aaaa" },{ "firstName": "Jason", "lastName":"Hunter", "email": "bbbb" },{ "firstName": "Elliotte", "lastName":"Harold", "email": "cccc" }],"authors":[{ "firstName": "Isaac", "lastName": "Asimov", "genre": "science fiction" },{ "firstName": "Tad", "lastName": "Williams", "genre": "fantasy" },{ "firstName": "Frank", "lastName": "Peretti", "genre": "christian fiction" }]
}
print('data1["programmers"][0]:',data1["programmers"][0])
print('data1["programmers"][0]["firstName"]:',data1["programmers"][0]['firstName'])
print('data1["authors"][1]["lastName"]:',data1["authors"][1]['lastName'])#输出结果:
data1["programmers"][0]: {'firstName': 'Brett', 'lastName': 'McLaughlin', 'email': 'aaaa'}
data1["programmers"][0]["firstName"]: Brett
data1["authors"][1]["lastName"]: Williams
data={"engineeringdata": {"sendDate": "","type": "1","project": {"IDproject": "01","projectName": "Golden Gate Bridge",},"systemInfo": {"os": {"osname": "win7","osversion": ""}}}
}
print('data["engineeringdata"]:',data["engineeringdata"])
print('data["engineeringdata"]["project"]:',data["engineeringdata"]["project"])
print('data["engineeringdata"]["project"]["IDproject"]:',data["engineeringdata"]["project"]['IDproject'])
print('data["engineeringdata"]["systemInfo"]["os"]["osname"]:',data["engineeringdata"]["systemInfo"]["os"]["osname"])
# k=data[0] 语法错误
#输出结果:
data["engineeringdata"]: {'sendDate': '', 'type': '1', 'project': {'IDproject': '01', 'projectName': 'Golden Gate Bridge'}, 'systemInfo': {'os': {'osname': 'win7', 'osversion': ''}}}
data["engineeringdata"]["project"]: {'IDproject': '01', 'projectName': 'Golden Gate Bridge'}
data["engineeringdata"]["project"]["IDproject"]: 01
data["engineeringdata"]["systemInfo"]["os"]["osname"]: win7
切记:中括号中为双引号。否则可能会报错
二、对json文件的保存与读取
一、将数据保存为.json文件
data={} #数据with open("a.json",'w',encoding='utf-8') as json_file:json.dump(data,json_file,ensure_ascii=False)
二、读取.json文件
data={} #存放读取的数据with open("a.json",'r',encoding='utf-8') as json_file:data=json.load(json_file)
三、对dump,dumps,load,loads的区分
一、概念理解
1、json.dumps()和json.loads()是json格式处理函数(可以这么理解,json是字符串)
(1)json.dumps()函数是将一个Python数据类型列表进行json格式的编码(可以这么理解,json.dumps()函数是将字典转化为字符串)
(2)json.loads()函数是将json格式数据转换为字典(可以这么理解,json.loads()函数是将字符串转化为字典)
2、json.dump()和json.load()主要用来读写json文件函数
二、代码测试
1 import json 2 3 # json.dumps()函数的使用,将字典转化为字符串 4 dict1 = {"age": "12"} 5 json_info = json.dumps(dict1) 6 print("dict1的类型:"+str(type(dict1))) 7 print("通过json.dumps()函数处理:") 8 print("json_info的类型:"+str(type(json_info))
运行截图:
2.py
1 import json 2 3 # json.loads函数的使用,将字符串转化为字典 4 json_info = '{"age": "12"}' 5 dict1 = json.loads(json_info) 6 print("json_info的类型:"+str(type(json_info))) 7 print("通过json.dumps()函数处理:") 8 print("dict1的类型:"+str(type(dict1)))
运行截图:
3.py
1 import json 2 3 # json.dump()函数的使用,将json信息写进文件 4 json_info = "{'age': '12'}" 5 file = open('1.json','w',encoding='utf-8') 6 json.dump(json_info,file)
运行截图(1.json文件):
4.py
1 import json 2 3 # json.load()函数的使用,将读取json信息 4 file = open('1.json','r',encoding='utf-8') 5 info = json.load(file) 6 print(info)
运行截图:
参考自https://www.cnblogs.com/xiaomingzaixian/p/7286793.html