使用jmeter時response中返回為json格式如下
{ "return_code": 0, "return_msg": "ok", "data": [ { "name": "武漢隆安置業有限公司", "id": 4, "product": "[{\"value\":\"7953AC76-A85A-E011-B371-001D096CF989\",\"text\":\"\\u9e7f\\u9e23\\u82d1\"}]" }, { "name": "湖北省建工房地產開發有限公司", "id": 10, "product": "[{\"value\":\"8C53AC76-A85A-E011-B371-001D096CF989\",\"text\":\"\\u5f90\\u4e1c\\u96c5\\u82d1\"}]" } ] }
需要將其中的"id": 4" id": 10,取出(4,10)用來后續斷言,由於每次返回id的組數可能都不同,故無法使用$$模板號來取
此時正則表達式匹配數字選擇-1來匹配所有符合的關聯值
查看結果樹時發現看不到所有id的值,通過添加后置處理器Debug PostProcessor查看提取的所有結果
當前提取出的結果:
custom_id_1=4 custom_id_1_g=1 custom_id_1_g0="id":4,"product" custom_id_1_g1=4 custom_id_2=10 custom_id_2_g=1 custom_id_2_g0="id":10,"product" custom_id_2_g1=10 custom_id_matchNr=2
可以通過邏輯控制器-for each控制器逐一取出關聯值
並且賦值給數組tmp,其中${custom_id_matchNr}為匹配到的關聯值個數
最后打印驗證下去取出的值是否正確
4與10被成功取出,證明tmp即為最終需要的數組
================================================================================================================================================
Jmeter正則實現探究:
當前提取出的結果:
custom_id_1=4
custom_id_1_g=1 custom_id_1_g0="id":4,"product" custom_id_1_g1=4 custom_id_2=10 custom_id_2_g=1 custom_id_2_g0="id":10,"product" custom_id_2_g1=10 custom_id_matchNr=2
可以發現取出的內容並不只是一個數組
下面還有分組 group1 group2
查看源碼實現
private void saveGroups(MatchResult result, String namep, JMeterVariables vars) { if (result != null) { for (int x = 0; x < result.groups(); x++) { vars.put(namep + "_g" + x, result.group(x)); //$NON-NLS-1$ } } }
List<MatchResult> collectAllMatches = new ArrayList<>(); try { PatternMatcher matcher = JMeterUtils.getMatcher(); PatternMatcherInput input = new PatternMatcherInput(textToMatch); while (matcher.contains(input, searchPattern)) { MatchResult match = matcher.getMatch(); collectAllMatches.add(match); } } finally { if (name.length() > 0){ vars.put(name + "_matchNr", Integer.toString(collectAllMatches.size())); //$NON-NLS-1$ } } if (collectAllMatches.size() == 0) { return defaultValue; } if (valueIndex.equals(ALL)) { StringBuilder value = new StringBuilder(); Iterator<MatchResult> it = collectAllMatches.iterator(); boolean first = true; while (it.hasNext()) { if (!first) { value.append(between); } else { first = false; } value.append(generateResult(it.next(), name, tmplt, vars)); } return value.toString(); } else if (valueIndex.equals(RAND)) { MatchResult result = collectAllMatches.get(ThreadLocalRandom.current().nextInt(collectAllMatches.size())); return generateResult(result, name, tmplt, vars); } else { try { int index = Integer.parseInt(valueIndex) - 1; MatchResult result = collectAllMatches.get(index); return generateResult(result, name, tmplt, vars); } catch (NumberFormatException e) { float ratio = Float.parseFloat(valueIndex); MatchResult result = collectAllMatches .get((int) (collectAllMatches.size() * ratio + .5) - 1); return generateResult(result, name, tmplt, vars); } catch (IndexOutOfBoundsException e) { return defaultValue; } }
custom_id_1_g=1
custom_id_2_g=1 // 代表group的長度
custom_id_2_g0="id":10,"product" //匹配的內容 custom_id_2_g1=10 //提取的內容