Jmeter之Json提取器详解(史上最全)
参考资料:https://www.bbsmax.com/A/D854lmBw5E/
Jsonpath在线测试:http://jsonpath.com/
实际工作中用到的一些场景:
- 提取某个特定的值
- 提取多个值
- 按条件取值
- 阵列取值(返回所有元素的列表/数组)
- 提取多个值
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
注意:
使用时,需要用下标读取,如${friend_id_1}、${friend_id_2}........
特别说明一下,如果想要获取返回元素的数量,可以用${friend_id_matchNr}
也可以使用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