在Python中我们做接口经常用到一些json的返回值我们常把他转化为字典,在前面的python数据类型详解(全面)中已经谈到对字典的的一些操作,今天我们就获取json返回值之后,然后转化为字典后的获取和其他的一些常用操作。
对字典的操作如下:
♦获取第一层字典中的数据:
1 dict = {'code': '200', 'message': '', 'redirect': '', 'value': {'name': '嗯嗯', 'title': '36', 'value': '123'}} 2 print(dict['code'])
结果输出: 200
♦获取第二层字典中的数据:如果我们要获取value的值,查看发现value后的数据也是一个字典
1 dict = {'code': '200', 'message': '', 'redirect': '', 'value': {'name': '嗯嗯', 'title': '36', 'value': '123'}} 2 print(dict['value']) 3 print(dict['value']['name'])
输出结果: {'name': '嗯嗯', 'title': '36', 'value': '123'} 嗯嗯
♦也可以通过dict.get()来获取键对应的值
1 dict = {'code': '200', 'message': '', 'redirect': '', 'value': {'name': '嗯嗯', 'title': '36', 'value': '123'}} 2 print(dict.get('code')) 3 print(dict.get('value').get('name')) 输出结果: 200 嗯嗯
♦dict.get()和dict['key']都可以用来获取键对应值,但是存在些许区别
1 dict = {'code': '200', 'message': '', 'redirect': '', 'value': {'name': '嗯嗯', 'title': '36', 'value': '123'}} 2 print(dict.get('wo')) 3 print(dict['wo'])
输出结果:
1 None # print(dict.get('wo'))
2 Traceback (most recent call last): 3 File "C:/Users/zy/Documents/GitHub/python3/searchTest/test.py", line 3, in <module>
print(dict['wo'])
KeyError: 'wo'#print(dict['wo'])
原因:dict['key']只能获取存在的值,如果不存在则触发KeyError;dict.get(key, default=None),返回指定键的值,如果值不在字典中返回默认值None
字典的值是一个list
♦dict = {'code': '200', 'message': '', 'redirect': '', 'value': [{'supplier': 'xyz', 'title': '我们在这里'}]}
我们发现value的数据和1中的情况不同,可以获取value的值判断是什么类型的数据
1 dict = {'code': '200', 'message': '', 'redirect': '', 'value': [{'supplier': 'xyz', 'title': '我们在这里'}]} 2 print(type(dict['value'])) 输出结果: <class 'list'>
根据列表特性 索引来获取list[0]查看数据,发现列表中的每个元素是字典,又可以根据字典的特性获取到supplier的值
1 dict = {'code': '200', 'message': '', 'redirect': '', 'value': [{'supplier': 'xyz', 'title': '我们在这里'}]} 2 print(dict['value'][0]) 3 print(type(dict['value'][0])) 输出结果: {'supplier': 'xyz', 'title': '我们在这里'} <class 'dict'>
1 dict = {'code': '200', 'message': '', 'redirect': '', 'value': [{'supplier': 'xyz', 'title': '我们在这里'}]} 2 print(dict['value'][0]['supplier']) 输出结果: xyz
字典基本操作
♦遍历字典
1 # 遍历字典
2 for key in dict: 3 print(key + ':' + str(dict[key])) 4
5 输出结果: 6 code:200
7 message: 8 redirect: 9 value:{'name': '嗯嗯', 'title': '36', 'value': '123'}
♦遍历字典的键key
1 #遍历字典的键key
2 for key in dict.keys(): 3 print(key) 4
5 输出结果: 6 code 7 message 8 redirect 9 value
♦遍历字典的值value
1 #遍历字典的值value
2 for value in dict.values(): 3 print(value) 4
5 输出结果: 6 200
7
8
9 {'name': '嗯嗯', 'title': '36', 'value': '123'}
♦遍历字典的项,item()方法把字典中每对key和value组成一个元组,并把这些元组放在列表中返回
1 #遍历字典的项,item()方法把字典中每对key和value组成一个元组,并把这些元组放在列表中返回
2 for item in dict.items(): 3 print(item) 4
5 输出结果: 6 ('code', '200') 7 ('message', '') 8 ('redirect', '') 9 ('value', {'name': '嗯嗯', 'title': '36', 'value': '123'})
♦基本操作
1 #修改键值
2 dict['message'] = 'ok'
3 print(dict) 4 #增加新的键/值
5 dict['wo'] = 'apple'
6 print(dict) 7 #输出键是'code'的条目
8 del dict['code'] 9 print(dict) 10 #清空字典所有条目
11 dict.clear() 12 print(dict) 13 #删除字典
14 del dict 15 print(dict) 16
17 输出结果: 18 {'code': '200', 'message': 'ok', 'redirect': '', 'value': {'name': '嗯嗯', 'title': '36', 'value': '123'}} 19 {'code': '200', 'message': 'ok', 'redirect': '', 'value': {'name': '嗯嗯', 'title': '36', 'value': '123'}, 'wo': 'apple'} 20 {'message': 'ok', 'redirect': '', 'value': {'name': '嗯嗯', 'title': '36', 'value': '123'}, 'wo': 'apple'} 21 {} 22 <class 'dict'>
转自:python3:jsonpath-rw处理Json对象,写的不错哦