【问题标题】:jMeter RegEx JSON responsejMeter 正则表达式 JSON 响应
【发布时间】:2021-12-06 13:46:29
【问题描述】:

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

这很快就解决了。谢谢!

【问题讨论】:

    标签: regex jmeter


    【解决方案1】:

    您使用的是.+?,这意味着:

    • . - 匹配任何不是换行符的单个字符(默认,可以使用 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»
    

    希望这会有所帮助。

    【讨论】:

    • 谢谢,这很有帮助。
    【解决方案2】:

    我建议你看看:

    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(这是我在调试采样器中看到的)。

    从 cmets 中的问题更新:

    如果您有值列表,即:

    {"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 结果的任何变量。

    【讨论】:

    • 蚂蚁,这很有趣。但是,由于某种原因,我无法让 mvn 编译该文件。有没有不用编译就可以下载的JAR文件?
    • @Dean 您可能正在使用 maven 2 来构建项目。你应该使用maven 3,抱歉没有在github页面中提到我会改变它
    • @Dean 你能建吗?
    • 嗨蚂蚁,对不起,在正则表达式工作后我没有机会构建。当我做一些更复杂的检查时,我会研究它
    • 嗨蚂蚁,我有 OS X Lion 并使用 RailsInstaller。它非常容易地构建它。我喜欢这些组件。但是,我在这里找不到有关各种组件的更多信息:atlantbh.com/jmeter-components。例如,JSON 提取组件上没有任何内容。
    【解决方案3】:

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

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

    【讨论】:

      猜你喜欢
      • 2012-09-29
      • 1970-01-01
      • 2019-08-01
      • 2018-03-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-18
      • 2014-11-09
      相关资源
      最近更新 更多