python:解析requests返回的response(json格式)說(shuō)明
我就廢話不多說(shuō)了,大家還是直接看代碼吧!
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 }}
補(bǔ)充知識(shí):使用Python的requests庫(kù)作接口測(cè)試——響應(yīng)結(jié)果處理
在實(shí)際工作中,很多接口的響應(yīng)都是json格式的數(shù)據(jù),在測(cè)試中需要對(duì)其進(jìn)行處理和分析。
設(shè)計(jì)到j(luò)son數(shù)據(jù)處理的方法有兩種:序列化和反序列化
python中序列化,簡(jiǎn)單講就是將python的字典轉(zhuǎn)換成json格式字符串,以便進(jìn)行儲(chǔ)存或者傳輸;
反序列化,簡(jiǎn)單講就是將json格式字符串轉(zhuǎn)換成python字典,用于對(duì)其進(jìn)行分析和處理。
JSON和DICT格式互轉(zhuǎn)方法:
import json # 序列化成json字符串d = {‘name’:‘jod’}j = json.dumps(d) #反序列化成字典print json.loads(j)
而在requests庫(kù)中,不用json.loads方法進(jìn)行反序列化,而是提供了響應(yīng)對(duì)象的json方法,用來(lái)對(duì)json格式的響應(yīng)體進(jìn)行反序列化
比如:
r = requests.get(url)r.json()
以上這篇python:解析requests返回的response(json格式)說(shuō)明就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. 如何在jsp界面中插入圖片2. ASP實(shí)現(xiàn)加法驗(yàn)證碼3. python selenium 獲取接口數(shù)據(jù)的實(shí)現(xiàn)4. ASP刪除img標(biāo)簽的style屬性只保留src的正則函數(shù)5. 詳解JSP 內(nèi)置對(duì)象request常見(jiàn)用法6. 利用ajax+php實(shí)現(xiàn)商品價(jià)格計(jì)算7. Python matplotlib 繪制雙Y軸曲線圖的示例代碼8. jsp EL表達(dá)式詳解9. JSP servlet實(shí)現(xiàn)文件上傳下載和刪除10. springboot集成與使用Sentinel的方法
