python:解析requests返回的response(json格式)說明
我就廢話不多說了,大家還是直接看代碼吧!
import requests, jsonr = requests.get(’http://192.168.207.160:9000/api/qualitygates/project_status?projectId=%s’ % (p_uuid) )state=json.loads(r.text).get(’projectStatus’).get(’status’)
返回如下:
{ 'projectStatus': { 'status': 'ERROR', 'conditions': [{ 'status': 'ERROR', 'metricKey': 'new_security_rating', 'comparator': 'GT', 'periodIndex': 1, 'errorThreshold': '1', 'actualValue': '5' }, { 'status': 'ERROR', 'metricKey': 'new_reliability_rating', 'comparator': 'GT', 'periodIndex': 1, 'errorThreshold': '1', 'actualValue': '4' }, { 'status': 'OK', 'metricKey': 'new_maintainability_rating', 'comparator': 'GT', 'periodIndex': 1, 'errorThreshold': '1', 'actualValue': '1' }, { 'status': 'ERROR', 'metricKey': 'new_coverage', 'comparator': 'LT', 'periodIndex': 1, 'errorThreshold': '80', 'actualValue': '0.0' }, { 'status': 'ERROR', 'metricKey': 'new_duplicated_lines_density', 'comparator': 'GT', 'periodIndex': 1, 'errorThreshold': '3', 'actualValue': '5.967688757006265' }], 'periods': [{ 'index': 1, 'mode': 'previous_version', 'date': '2019-05-31T09:35:58+0800' }], 'ignoredConditions': false }}
補充知識:使用Python的requests庫作接口測試——響應結果處理
在實際工作中,很多接口的響應都是json格式的數(shù)據(jù),在測試中需要對其進行處理和分析。
設計到json數(shù)據(jù)處理的方法有兩種:序列化和反序列化
python中序列化,簡單講就是將python的字典轉換成json格式字符串,以便進行儲存或者傳輸;
反序列化,簡單講就是將json格式字符串轉換成python字典,用于對其進行分析和處理。
JSON和DICT格式互轉方法:
import json # 序列化成json字符串d = {‘name’:‘jod’}j = json.dumps(d) #反序列化成字典print json.loads(j)
而在requests庫中,不用json.loads方法進行反序列化,而是提供了響應對象的json方法,用來對json格式的響應體進行反序列化
比如:
r = requests.get(url)r.json()
以上這篇python:解析requests返回的response(json格式)說明就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持好吧啦網(wǎng)。
相關文章:
1. python多線程和多進程關系詳解2. 詳解瀏覽器的緩存機制3. Xml簡介_動力節(jié)點Java學院整理4. Python 實現(xiàn)勞拉游戲的實例代碼(四連環(huán)、重力四子棋)5. 一款功能強大的markdown編輯器tui.editor使用示例詳解6. JSP之表單提交get和post的區(qū)別詳解及實例7. Python xlrd/xlwt 創(chuàng)建excel文件及常用操作8. 存儲于xml中需要的HTML轉義代碼9. .Net加密神器Eazfuscator.NET?2023.2?最新版使用教程10. ASP動態(tài)網(wǎng)頁制作技術經(jīng)驗分享
