【原创】Jmeter正则表达式提取json中多个关联值
使用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 //提取的内容