久久r热视频,国产午夜精品一区二区三区视频,亚洲精品自拍偷拍,欧美日韩精品二区

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

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

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

我就廢話(huà)不多說(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)。

標(biāo)簽: Python 編程
相關(guān)文章:
主站蜘蛛池模板: 枝江市| 濮阳县| 万盛区| 白朗县| 焦作市| 大邑县| 永宁县| 那曲县| 泽库县| 洞口县| 荔浦县| 丹江口市| 同江市| 娄烦县| 井研县| 德州市| 中卫市| 通榆县| 澄江县| 通化县| 岳阳县| 江安县| 宣汉县| 三都| 托克托县| 峡江县| 湘乡市| 蕉岭县| 湟源县| 和静县| 张家口市| 朝阳市| 额尔古纳市| 翁牛特旗| 黄陵县| 镇康县| 上高县| 清涧县| 福鼎市| 安国市| 湛江市|