8

我已经阅读了一些关于 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}}

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

在此处输入图像描述

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

2012 年 6 月 26 日更新

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

我还在 JMX 文件中找到了一个替换

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

并替换为

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

这使得修复很快。谢谢!

4

3 回答 3

14

您正在使用.+?这意味着:

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

所以,虽然它会匹配"id":26模式匹配.+?,但它2只是而不是26.

要解决此问题,您可以尝试比这更好的方法:

  ("id":\d+)\b

方法

// ("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»

或者

  ("id":[^,:]+)\b

方法

// ("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»

或者

("id":\S+)\b

方法

// ("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»

希望这可以帮助。

于 2012-06-16T05:07:08.757 回答
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}}

JSON 提取非常简单:

$.address.id

这样就不需要花哨的正则表达式了。结果是 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}]}

具有 2 个地址的列表,1 个具有 id 26,另一个具有 6。Json 路径$.address.id应返回这两个 id。我刚刚看到了采样器源代码,但无法获得计数,但是您可以通过在示例中添加另一个后处理器来做到这一点,即BSF Sampler并添加以下代码:

vars.put("ADDRESS_COUNT", "${__javaScript('${add}'.split('\,').length,)}".toString());

${add}存储结果的变量在哪里$.address.id

于 2012-06-26T09:05:24.167 回答
0

在 jmeter 中选择 JSON 响应的最佳方法是执行类似 (?m) "nodeRef": "workspace://SpacesStore/idSpaceStore",\s* "name": "folder_for_testing-1372432881900",

(?m) - 表示开始将正则表达式视为面向多行的 (\s*) - 表示任何字符

于 2013-06-28T16:05:35.973 回答