JMeter从Response JSON体中取出数组值循环取值并判断逐一取值

https://www.cnblogs.com/LT-XILI/p/11643335.html

https://www.cnblogs.com/linxinmeng/p/12696339.html

正则取值:

样体:

[{"lineId":"9cb9042d770547ab927970f99dc9108a","gwId":"SV-K5BU6521","gwName":"box_105"},{"lineId":"1b336475ce60463abb02a4dde55dcb10","gwId":"SV-K5BW9696","gwName":"box 118"},{"lineId":"b4d65aa5a3a84012b2a958ead061bb24","gwId":"SV-K5BU6557","gwName":"box 111"},{"lineId":"ff3ca1693b2985c272c91838e28e0a3d","gwId":"SV-K5BW9680","gwName":"box 114"},{"lineId":"55a40a056fd348f3a5766cc33e359ff9","gwId":"SV-K5BT3724","gwName":"box 116"},{"lineId":"de61a6b9a2797149906f2eb2a957588d","gwId":"SV-K5BU6553","gwName":"box_115"},{"lineId":"1911527525094e0ed2f36a3ee1f3709e","gwId":"SV-K5AC7569","gwName":"box 22"}]

一、取值

法1:正则取值

正则表达式:"gwId":"(.*?)",

匹配数字:-1 代表全部取出

 

JMeter从Response JSON体中取出数组值循环取值并判断逐一取值

 

法二:

JSON提取器

$[*].gwId

JMeter从Response JSON体中取出数组值循环取值并判断逐一取值

二、查看Debug Sampler查后变量

JMeterVariables:
JMeterThread.last_sample_ok=true
[email protected]2
START.HMS=111135
START.MS=1589253095590
START.YMD=20200512
TESTSTART.MS=1589267924851
__jm__ForEach Controller__idx=0
__jm__TEST__idx=0
__jmeter.U_T__=TEST 1-1
__jmv_SAME_USER=true
box_id_1=SV-K5BU6521
box_id_2=SV-K5BW9696
box_id_3=SV-K5BU6557
box_id_4=SV-K5BW9680
box_id_5=SV-K5BT3724
box_id_6=SV-K5BU6553
box_id_7=SV-K5AC7569
box_id_matchNr=7
box_id_new_1=SV-K5BU6521
box_id_new_1_g=1
box_id_new_1_g0="gwId":"SV-K5BU6521",
box_id_new_1_g1=SV-K5BU6521
box_id_new_2=SV-K5BW9696
box_id_new_2_g=1
box_id_new_2_g0="gwId":"SV-K5BW9696",
box_id_new_2_g1=SV-K5BW9696
box_id_new_3=SV-K5BU6557
box_id_new_3_g=1
box_id_new_3_g0="gwId":"SV-K5BU6557",
box_id_new_3_g1=SV-K5BU6557
box_id_new_4=SV-K5BW9680
box_id_new_4_g=1
box_id_new_4_g0="gwId":"SV-K5BW9680",
box_id_new_4_g1=SV-K5BW9680
box_id_new_5=SV-K5BT3724
box_id_new_5_g=1
box_id_new_5_g0="gwId":"SV-K5BT3724",
box_id_new_5_g1=SV-K5BT3724
box_id_new_6=SV-K5BU6553
box_id_new_6_g=1
box_id_new_6_g0="gwId":"SV-K5BU6553",
box_id_new_6_g1=SV-K5BU6553
box_id_new_7=SV-K5AC7569
box_id_new_7_g=1
box_id_new_7_g0="gwId":"SV-K5AC7569",
box_id_new_7_g1=SV-K5AC7569
box_id_new_matchNr=7
box_name=SV-K5BU6521
box_sn=SV-K5BU6553
ip=192.168.20.209

变量:box_id_matchNr=7 就是计数的

三、在ForEach里循环取值

box_id为前缀

JMeter从Response JSON体中取出数组值循环取值并判断逐一取值

四、在if控制器里做判断

用到变量 box_sn

JMeter从Response JSON体中取出数组值循环取值并判断逐一取值

表达式:

${__jexl3("${box_name}"!="${box_sn}",)} 

JMeter从Response JSON体中取出数组值循环取值并判断逐一取值 ${__jexl3("${box_name}"=="${box_sn}",)}

JMeter从Response JSON体中取出数组值循环取值并判断逐一取值

JMeter从Response JSON体中取出数组值循环取值并判断逐一取值

五、运行结果

JMeter从Response JSON体中取出数组值循环取值并判断逐一取值 只执行一次:因为box_sn=SV-K5BU6553

 

小技巧:

可以在函数助手:

也可以用__V函数

函数助手在:

Tool->函数助手对话框

JMeter从Response JSON体中取出数组值循环取值并判断逐一取值

来直接生成函数,保证其正确性

JMeter从Response JSON体中取出数组值循环取值并判断逐一取值

JMeter从Response JSON体中取出数组值循环取值并判断逐一取值JMeter从Response JSON体中取出数组值循环取值并判断逐一取值

另法2:

循环控制器+计数器+ If控制器

循环控制器 Loop:

JMeter从Response JSON体中取出数组值循环取值并判断逐一取值

计数器:

JMeter从Response JSON体中取出数组值循环取值并判断逐一取值

If控制器

表达式:${__jexl3("${box_sn}"=="${__V(box_id_${num},)}",)}

JMeter从Response JSON体中取出数组值循环取值并判断逐一取值

拼接测试:拼接${__V(box_id_${num},)}

JMeter从Response JSON体中取出数组值循环取值并判断逐一取值

另法3:

另法2:

 While 循环控制器:

While 循环控制器

表达式:${__jexl3("${box_sn}"=="${__V(box_id_${num},)}",)}

JMeter从Response JSON体中取出数组值循环取值并判断逐一取值

拼接测试:拼接${__V(box_id_${num},)}

JMeter从Response JSON体中取出数组值循环取值并判断逐一取值

 

 

参考:

Jmeter 控制器遍历数组参数

https://blog.****.net/hanlinbaobei/article/details/94594183?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.nonecase&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.nonecase

https://blog.****.net/weixin_30521161/article/details/96360395

https://blog.****.net/qq_39413365/article/details/100041035

https://www.cnblogs.com/linxinmeng/p/12696339.html

https://blog.****.net/weixin_38631081/article/details/79806545