jMeter RegEx JSON响应

Dea*_*ean 7 regex jmeter

我已经阅读了一些关于jMeter RegEx提取的教程,但是它没有用.我正在使用jMeter 2.7.

我有这个JSON:

{"address":{"id":26,"user_id":1,"genre":"billing","first_name":"testFN1","last_name":"testLN1","company":null,"street1":null,"street2":null,"city":null,"state":"DC","zip":null,"country":null,"country_iso2":null,"phone1":"32432424322","phone2":null}}
Run Code Online (Sandbox Code Playgroud)

和这个RegEx Extractor:"id":(.+?),这里是jMeter的截图

在此输入图像描述

对于提取,我得到$new_address_id= 2,而不是26.有任何想法吗?

更新2012年6月26日:

谢谢Cylian的建议.这非常有帮助.我最终将其更改为:"id":(\d+).

我也在JMX文件中找到了替换

<stringProp name="RegexExtractor.regex">&quot;id&quot;:(.+?,)</stringProp>
Run Code Online (Sandbox Code Playgroud)

并替换为

<stringProp name="RegexExtractor.regex">&quot;id&quot;:(\d+)</stringProp>
Run Code Online (Sandbox Code Playgroud)

这使得修复很快.谢谢!

Cyl*_*ian 14

您正在使用.+?哪种方式:

  • .- 匹配任何不是换行符的单个字符(默认情况下,可以使用s标志更改)
  • + - 在一次和无限次之间匹配前一个字符
  • ? - 尽可能少(懒惰)

因此,尽管它要匹配"id":26的模式匹配,.+?2的只是代替26.

要解决此问题,请尝试更好的方法:

  ("id":\d+)\b
Run Code Online (Sandbox Code Playgroud)

手段

// ("id":\d+)\b
// 
// Options: case insensitive
// 
// Match the regular expression below and capture its match into backreference number 1 «("id":\d+)»
//    Match the characters “"id":” literally «"id":»
//    Match a single digit 0..9 «\d+»
//       Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
// Assert position at a word boundary «\b»
Run Code Online (Sandbox Code Playgroud)

要么

  ("id":[^,:]+)\b
Run Code Online (Sandbox Code Playgroud)

手段

// ("id":[^,:]+)\b
// 
// Options: case insensitive
// 
// Match the regular expression below and capture its match into backreference number 1 «("id":[^,:]+)»
//    Match the characters “"id":” literally «"id":»
//    Match a single character NOT present in the list “,:” «[^,:]+»
//       Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
// Assert position at a word boundary «\b»
Run Code Online (Sandbox Code Playgroud)

要么

("id":\S+)\b
Run Code Online (Sandbox Code Playgroud)

手段

// ("id":\S+)\b
// 
// Options: case insensitive
// 
// Match the regular expression below and capture its match into backreference number 1 «("id":\S+)»
//    Match the characters “"id":” literally «"id":»
//    Match a single character that is a “non-whitespace character” «\S+»
//       Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
// Assert position at a word boundary «\b»
Run Code Online (Sandbox Code Playgroud)

希望这可以帮助.


ant*_*ant 12

我建议你看看:

http://jmeter-plugins.org/wiki/JSONPathExtractor/

本节(JSON utils(JSON Path Assertion,JSON Path Extractor,JSON Formatter))特别针对这种情况.这些是我公司开发的一套jmeter工具,它们非常有用.

我们以你的情况为例.测试用例如下所示:

在此输入图像描述

因此,虚拟样本返回响应,就像您指定的那样:

{"address":{"id":26,"user_id":1,"genre":"billing","first_name":"testFN1","last_name":"testLN1","company":null,"street1":null,"street2":null,"city":null,"state":"DC","zip":null,"country":null,"country_iso2":null,"phone1":"32432424322","phone2":null}}
Run Code Online (Sandbox Code Playgroud)

JSON提取非常简单:

$.address.id
Run Code Online (Sandbox Code Playgroud)

在那里你不需要花哨的正则表达式.结果是26(这是我在调试采样器中看到的).

在评论中更新问题:

如果你有值列表,即:

{"address":[{"id":26,"user_id":1,"genre":"billing","first_name":"testFN1","last_name":"testLN1","company":null,"street1":null,"street2":null,"city":null,"state":"DC","zip":null,"country":null,"country_iso2":null,"phone1":"32432424322","phone2":null}, {"id":6,"user_id":1,"genre":"billing","first_name":"testFN1","last_name":"testLN1","company":null,"street1":null,"street2":null,"city":null,"state":"DC","zip":null,"country":null,"country_iso2":null,"phone1":"32432424322","phone2":null}]}
Run Code Online (Sandbox Code Playgroud)

具有2个地址的列表,1具有id 26和另一个6. Json Path $.address.id应该返回这两个id.我刚刚看到了采样器源代码,但是无法获得计数,但是你可以通过在样本中添加另一个后处理器来实现,即BSF Sampler添加以下代码:

vars.put("ADDRESS_COUNT", "${__javaScript('${add}'.split('\,').length,)}".toString());
Run Code Online (Sandbox Code Playgroud)

哪里${add}是您存储的结果的任何变量$.address.id.