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


https://www.jb51.net/article/181946.htm

 

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

 更新時間:2020年04月30日 09:59:54   作者:秋尋草  
 
這篇文章主要介紹了python:解析requests返回的response(json格式)說明,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
 

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

1
2
3
import requests, json
state = json.loads(r.text).get( 'projectStatus' ).get( 'status' )

返回如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
{
  "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格式互轉方法:

1
2
3
4
5
6
7
8
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格式)說明就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持腳本之家。


免責聲明!

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



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