在JMETER中循环遍历JSON响应+ [英] Looping through JSON response + in JMETER

查看:483
本文介绍了在JMETER中循环遍历JSON响应+的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Jmeter进行性能测试,并停留在以下几点: 我从Webapi收到JSON响应,如下所示:

I am using Jmeter for performance testing and stuck at following point: I am getting a JSON response from Webapi as follows:

PersonInfoList:
Person
[0]
{
  id: 1
  name: Steve
}
[1]
Person
{
  id: 2
  name: Mark
}

我需要基于此JSON数组的计数获取ID,并创建一个以逗号分隔的字符串作为("Expected value" = 1,2)

I need to get the ids based on the count of this JSON array and create a comma separated string as ("Expected value" = 1,2)

我知道如何使用JSON Post处理器或Regex处理器读取特定的元素,但是无法遍历数组并按说明创建字符串,因此我可以在下一个采样器请求中使用此值.

I know how to read a particular element using JSON Post processor or Regex processor but am unable to loop through the array and create a string as explained so that I can use this value in my next sampler request.

请帮我解决这个问题:我正在使用Jmeter 3.0,如果可以在不使用外部第三方库的情况下实现这一点,那就太好了.抱歉,上面的JSON语法

Please help me out with this: I am using Jmeter 3.0 and if this could be achieved without using external third party libs that would be great. Sorry for the JSON syntax above

推荐答案

出现在JMeter 3.0中的> JSON Path PostProcessor .为了获取单个变量中的所有值,请按如下所示配置JSON Path PostProcessor:

Actually similar functionality comes with JSON Path PostProcessor which appeared in JMeter 3.0. In order to get all the values in a single variable configure JSON Path PostProcessor as follows:

  • 变量名称:任何有意义的名称,例如id
  • JSON路径表达式:$..id或用于提取ID的任何内容
  • 比赛号码:-1
  • 计算串联变量(后缀_ALL):检查
  • Variable Names: anything meaningful, i.e. id
  • JSON Path Expressions: $..id or whatever you use to extract the ids
  • Match Numbers: -1
  • Compute concatenation var (suffix _ALL): check

因此,您将获得id_ALL变量,该变量将包含所有JSON路径表达式匹配项(以逗号分隔)

As a result you'll get id_ALL variable which will contain all JSON Path expression matches (comma-separated)

更多通用"答案将适用于任何其他提取器类型,并且实际上将允许连接使用脚本编写的任意JMeter变量(此外,如果需要此期望值和括号")

More "universal" answer which will be applicable for any other extractor types and in fact will allow to concatenate any arbitrary JMeter Variables is using scripting (besides if you need this "expected value and parentheses)

为了将名称以"id"开头的所有变量连接到单个字符串中,请在JSON Path PostProcessor之后的某个位置添加Beanshell PostProcessor,并将以下代码放入脚本"区域

In order to concatenate all variables which names start with "id" into a single string add Beanshell PostProcessor somewhere after JSON Path PostProcessor and put the following code into "Script" area

StringBuilder result = new StringBuilder();
result.append("(\"Expected value\" = ");
Iterator iterator = vars.getIterator();

while (iterator.hasNext()) {
  Map.Entry e = (Map.Entry) iterator.next();
  if (e.getKey().matches("id_(\\d+)")) {
      result.append(e.getValue());
      result.append(",");
  }
}
result.append(")");
vars.put("expected_value", result.toString());

以上代码会将结果字符串存储到${expected value} JMeter变量中.请参见如何使用BeanShell:JMeter最喜欢的内置组件有关使用脚本绕过JMeter限制以及使用Beanshell测试元素中的JMeter和Java API的更多信息.

Above code will store the resulting string into ${expected value} JMeter Variable. See How to Use BeanShell: JMeter's Favorite Built-in Component article for more information regarding bypassing JMeter limitations using scripting and using JMeter and Java API from Beanshell test elements.

演示:

这篇关于在JMETER中循环遍历JSON响应+的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆