您的位置:首頁技術文章
文章詳情頁

python:解析requests返回的response(json格式)說明

瀏覽:5日期:2022-07-27 08:09:40

我就廢話不多說了,大家還是直接看代碼吧!

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格式的數據,在測試中需要對其進行處理和分析。

設計到json數據處理的方法有兩種:序列化和反序列化

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格式)說明就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持好吧啦網。

標簽: Python 編程
相關文章:
国产综合久久一区二区三区