一、背景:
同一个正则表达式匹配多个响应结果值,之前都是添加多个正则表达式,一个一个去获取需要的值,比较麻烦;今天尝试了一下用一个正则表达式获取响应中所有需要的值,使用这种方式也能够获取多个结果中指定的值,完美省事!
二、例如:我要获取如下响应报文中首次出现的两个值(barnType 和 trainScheduleBarnId 的值)目标是:{"available":0,"barnNo":"01","barnType":"STX1","recommend":0,"spaceStatus":"FULL","trainScheduleBarnId":3271}
三、接口响应的所有结果:
{"content":{"barns":[{"available":0,"barnNo":"01","barnType":"STX1","recommend":0,"spaceStatus":"FULL","trainScheduleBarnId":3271},{"available":1,"barnNo":"02","barnType":"STX2","recommend":1,"spaceStatus":"EMPTY","trainScheduleBarnId":3272},{"available":0,"barnNo":"03","barnType":"STX3","recommend":0,"spaceStatus":"EMPTY","trainScheduleBarnId":3273},{"available":0,"barnNo":"04","barnType":"STX4","recommend":0,"spaceStatus":"EMPTY","trainScheduleBarnId":3274},{"available":1,"barnNo":"05","barnType":"STX1","recommend":0,"spaceStatus":"EMPTY","trainScheduleBarnId":3275},{"available":1,"barnNo":"06","barnType":"STX2","recommend":0,"spaceStatus":"EMPTY","trainScheduleBarnId":3276},{"available":0,"barnNo":"07","barnType":"STX3","recommend":0,"spaceStatus":"EMPTY","trainScheduleBarnId":3277},{"available":0,"barnNo":"08","barnType":"STX4","recommend":0,"spaceStatus":"EMPTY","trainScheduleBarnId":3278}],"count":3,"endStationId":2101,"endStationName":"石家庄","endTime":1525491060000,"id":414,"startStationId":2401,"startStationName":"太原","startTime":1525059060000,"trainNo":"K707"},"errorCode":null,"errorMsg":null,"status":"OK"}
四、正则表达式的写法:
五、调试的时候添加一个Debug PostProcessor 调试器
六、在Debug PostProcessor中可以查看到获取的参数结果
七、引用方式:${testValues_g1} 和 ${testValues_g2}
八、这里还有个小技巧:返回响应结果顺序都是不变的,想每次获取不同参数的值,怎么办?只需要小小的改动即可
九、匹配数字的含义:
* 0 表示随机
* -1 表示全部
* 1表示第一个
* 2表示第二个
* ......