Jmeter之Json提取器詳解(史上最全)


參考資料:https://www.bbsmax.com/A/D854lmBw5E/

Jsonpath在線測試:http://jsonpath.com/


實際工作中用到的一些場景:

  1. 提取某個特定的值
  2. 提取多個值
  3. 按條件取值
  4. 陣列取值(返回所有元素的列表/數組)
  5. 提取多個值

Jsonpath提取器需要另外安裝

  • 下載plugins-manager.jar加入其加入JMETER_HOME/lib/ext目錄,
  • 重新啟動JMeter,
  • 點擊Options > Plugins Manager頂部菜單,
  • 選擇Available Plugins標簽,
  • 選擇Json Plugins並雙擊應用更改並重新啟動JMeter。

使用方法:

Json Path提取器應放在HTTP Sampler下。可設置的參數有:

  • 變量名稱:分號單獨的變量名稱,
  • JSON Path Expressions:從json響應中提取內容的表達式,
  • 匹配數字:-1對於所有,0對於隨機的,n對於第n個,
  • Compute concatenation var:創建一個${foo_ALL}包含所有提取值的串聯的變量,
  • 默認值:如果表達式不適用於正在處理的json文檔,使用此處給定的默認值。

接下來用一個實際案例來演示Json提取的一些用法:

示例:有如下json

{
    "code": 0,
    "data": {
        "total": 10,
        "pageNo": 1,
        "pageSize": 100,
        "items": [
            {
                "friendHeadLogoUrl": null,
                "msg": "你好,我仰慕你的才華!",
                "shield": 0,
                "star": 0,
                "friendNickname": "Auto",
                "origin": 202,
                "black": 0,
                "memo": null,
                "updateTime": 1591169560056,
                "type": 2,
                "userId": 1268082752079560705,
                "friendId": 1267735865761759234,
                "createTime": 1591169490879,
                "blackUpdateTime": 0,
                "addUpdateTime": 1591169560056,
                "id": 1268082870707040258,
                "close": 0,
                "status": 2
            },
            {
                "friendHeadLogoUrl": null,
                "msg": "你好,我仰慕你的才華!",
                "shield": 0,
                "star": 0,
                "friendNickname": "Auto",
                "origin": 202,
                "black": 0,
                "memo": null,
                "updateTime": 1591169560092,
                "type": 2,
                "userId": 1268082752079560705,
                "friendId": 1267735866185383938,
                "createTime": 1591169491390,
                "blackUpdateTime": 0,
                "addUpdateTime": 1591169560092,
                "id": 1268082872854523906,
                "close": 0,
                "status": 2
            },
            {
                "friendHeadLogoUrl": null,
                "msg": "你好,我仰慕你的才華!",
                "shield": 0,
                "star": 0,
                "friendNickname": "Auto",
                "origin": 202,
                "black": 0,
                "memo": null,
                "updateTime": 1591169560126,
                "type": 2,
                "userId": 1268082752079560705,
                "friendId": 1267735867363983362,
                "createTime": 1591169491862,
                "blackUpdateTime": 0,
                "addUpdateTime": 1591169560126,
                "id": 1268082874830041089,
                "close": 0,
                "status": 2
            },
            {
                "friendHeadLogoUrl": null,
                "msg": "你好,我仰慕你的才華!",
                "shield": 0,
                "star": 0,
                "friendNickname": "Auto",
                "origin": 202,
                "black": 0,
                "memo": null,
                "updateTime": 1591169560164,
                "type": 2,
                "userId": 1268082752079560705,
                "friendId": 1267735867825356802,
                "createTime": 1591169492296,
                "blackUpdateTime": 0,
                "addUpdateTime": 1591169560164,
                "id": 1268082876654563330,
                "close": 0,
                "status": 2
            },
            {
                "friendHeadLogoUrl": null,
                "msg": "你好,我仰慕你的才華!",
                "shield": 0,
                "star": 0,
                "friendNickname": "Auto",
                "origin": 202,
                "black": 0,
                "memo": null,
                "updateTime": 1591169560310,
                "type": 2,
                "userId": 1268082752079560705,
                "friendId": 1267735867003273217,
                "createTime": 1591169493876,
                "blackUpdateTime": 0,
                "addUpdateTime": 1591169560310,
                "id": 1268082883277369345,
                "close": 0,
                "status": 2
            },
            {
                "friendHeadLogoUrl": null,
                "msg": "你好,我仰慕你的才華!",
                "shield": 0,
                "star": 0,
                "friendNickname": "Auto",
                "origin": 202,
                "black": 0,
                "memo": null,
                "updateTime": 1591169560376,
                "type": 2,
                "userId": 1268082752079560705,
                "friendId": 1267735868571942913,
                "createTime": 1591169494629,
                "blackUpdateTime": 0,
                "addUpdateTime": 1591169560376,
                "id": 1268082886439874561,
                "close": 0,
                "status": 2
            }
        ],
        "timestamp": null
    },
    "codeMsg": "success"
}

1、提取某個特定的值

Jsonpath 結果
$.data.total 10
$..total 10
$..items[1].userId 1268082752079560705

2、提取多個值

  • 比如要提取items列表里所有的friendId

$..friendId

$..[*].friendId

$.data.items[*].friendId

三種寫法都可以,結果為:

Result[0]=1267735865761759234
Result[1]=1267735866185383938
Result[2]=1267735867363983362
Result[3]=1267735867825356802
Result[4]=1267735864650268673
Result[5]=1267735865363300353
Result[6]=1267735866688700417
Result[7]=1267735867003273217
Result[8]=1267735868232204289
Result[9]=1267735868571942913

注意:

  1. 使用時,需要用下標讀取,如${friend_id_1}、${friend_id_2}........

  2. 特別說明一下,如果想要獲取返回元素的數量,可以用${friend_id_matchNr}

  3. 也可以使用foreach控制器循環讀取,這里不再描述。

3、按條件提取值

有時候只需要提取某個特定條件下的參數,比如現在想要提取創建時間為1591169490879的friendId

$.data.items[?(@.createTime ==1591169490879)].friendId

4、陣列提取

提取任何節點的某個key的值,比如提取friendId
$..friendId

結果為:

friend_all_1=1267735865761759234
friend_all_10=1267735868571942913
friend_all_2=1267735866185383938
friend_all_3=1267735867363983362
friend_all_4=1267735867825356802
friend_all_5=1267735864650268673
friend_all_6=1267735865363300353
friend_all_7=1267735866688700417
friend_all_8=1267735867003273217
friend_all_9=1267735868232204289
friend_all_ALL=1267735865761759234,1267735866185383938,1267735867363983362,1267735867825356802,1267735864650268673,1267735865363300353,1267735866688700417,1267735867003273217,1267735868232204289,1267735868571942913
friend_all_matchNr=10

_ALL下標是將所有元素組成一個list,像后續接口有時候參數是以數組形式來傳參,這個函數就很有用,如果需要返回此函數,需要在json提取器中,勾選Compute concatenation var

5、提取多個值

json表達式:

$..['msg','friendId']

返回的變量如下:

two_1={"msg":"你好,我仰慕你的才華!","friendId":1267735865761759234}
two_10={"msg":"你好,我仰慕你的才華!","friendId":1267735868571942913}
two_2={"msg":"你好,我仰慕你的才華!","friendId":1267735866185383938}
two_3={"msg":"你好,我仰慕你的才華!","friendId":1267735867363983362}
two_4={"msg":"你好,我仰慕你的才華!","friendId":1267735867825356802}
two_5={"msg":"你好,我仰慕你的才華!","friendId":1267735864650268673}
two_6={"msg":"你好,我仰慕你的才華!","friendId":1267735865363300353}
two_7={"msg":"你好,我仰慕你的才華!","friendId":1267735866688700417}
two_8={"msg":"你好,我仰慕你的才華!","friendId":1267735867003273217}
two_9={"msg":"你好,我仰慕你的才華!","friendId":1267735868232204289}
two_ALL={"msg":"你好,我仰慕你的才華!","friendId":1267735865761759234},{"msg":"你好,我仰慕你的才華!","friendId":1267735866185383938},{"msg":"你好,我仰慕你的才華!","friendId":1267735867363983362},{"msg":"你好,我仰慕你的才華!","friendId":1267735867825356802},{"msg":"你好,我仰慕你的才華!","friendId":1267735864650268673},{"msg":"你好,我仰慕你的才華!","friendId":1267735865363300353},{"msg":"你好,我仰慕你的才華!","friendId":1267735866688700417},{"msg":"你好,我仰慕你的才華!","friendId":1267735867003273217},{"msg":"你好,我仰慕你的才華!","friendId":1267735868232204289},{"msg":"你好,我仰慕你的才華!","friendId":1267735868571942913}
two_matchNr=10


免責聲明!

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



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