記錄在 jmeter 介面測試中 json 返回數值幾種特殊正則提取

蕭帥發表於2020-06-15

第一種正則提取
{"code":0,"msg":null,"data":[{"word":"華法林鈉片","type":"mix","ids":{"S779":[816,873],"S538":[816,873],"S417":[816,873],"S018":[816,873],"S898":[816,873],"S899":[816,873],"S410":[816,873],"S895":[816,873],"SA33":[816,873],"SA34":[816,873]},"moreDrug":0}]}

提取上面介面返回json中的ids值作為下一個介面ids入參值,百度了很多沒有找到相關結果,之前把正則提取想複雜了,所有找不到好的解決方法,後來想想一下我是不是可以直接提取整個ids作為下一介面入參值

解決方案:"ids":{(.*?)}

第二種正則提取
{"code":0,"msg":null,"data":[{"word":"華法林鈉片","type":"mix","ids":{"S779":[816,873],"S538":[816,873],"S417":[816,873],"S018":[816,873],"S898":[816,873],"S899":[816,873],"S410":[816,873],"S895":[816,873],"SA33":[816,873],"SA34":[816,873]},"moreDrug":0}]}

根據下一介面入參,只需ids陣列前一個雙引號裡的值,那該咋整,之間百度過很多資料,也沒有類似的案例,也是通過自己一點點摸索出來的,我是小白

解決方案:ids\":{\"(\w+)\"

第三種正則提取

"storeTickets":[
{
"storeNo":"BZLD0002",
"ticketId":35,
"utId":null,
"utStatus":0,
"isAllSickness":0,
"packageId":674,
"type":3,
"title":"寶芝林門店88折",
"subTitle":null,
"remark":null,
"code":null,
"qrcode":null,
"rule":null,
"amount":0,
"hasConsumeLimit":0,
"consumeLimit":null,
"miniConsume":0,
"discount":0.89,
"discountTag":null,
"discountAmountLimit":50,
"startTime":1591703230,
"endTime":1625053633,
"createdAt":null,
"availableNum":10,
"limitDrawNum":3,
"limitDayDrawNum":1,
"duration":null,
"expireTime":1625053654,
"ticketTag":"8.9折券",
"texpireTime":null
},

同時提取ticketId,packageId用於下一介面入參,但是由於返回時ticketId和packageId中間還有utId,utStatus,isAllSickness等引數,甚至還有換行空格等,所以去匹配時需要抹掉中間不需要的欄位,使用正則:[.\s\S]+? 代表所有的字元,

完整的正則就是:"ticketId":(\d+),[.\s\S]+?"packageId":(\d+)

相關文章