python3字典:獲取json響應值來進行斷言的用法詳解


在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對象,寫的不錯哦


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM