jMeter RegEx JSON 响应 [英] jMeter RegEx JSON response

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

问题描述

我已经阅读了一些关于 jMeter RegEx 提取的教程,但它不起作用.我使用的是 jMeter 2.7.

I've read a few tutorials on jMeter RegEx extraction but it's not working. I'm using 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 的截图

and this RegEx Extractor: "id":(.+?), here's the screenshot of jMeter

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

For the extraction, I get $new_address_id = 2, instead of 26. Any ideas?

2012 年 6 月 26 日更新:

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

Thanks Cylian for your suggestion. It is very helpful. I ended up changing it to: "id":(d+).

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

I also did a find a replace in the JMX file for

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

并替换为

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

快速修复.谢谢!

推荐答案

建议你看看:

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

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

This section (JSON utils (JSON Path Assertion, JSON Path Extractor, JSON Formatter)) in particular for this case. These are set of jmeter tools developed in my company, they're very useful.

让我们以您的案例为例.测试用例如下所示:

Let's take your case as example. The test case looks like this :

所以虚拟样本返回响应,就像你指定的一样:

So dummy sample returns response, just like the one you specified :

{"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 提取非常简单:

JSON extraction is very simple :

$.address.id

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

And there you go no need for fancy regexes. Result is 26 (that is what I see in debug sampler).

从评论中的问题更新:

如果您有值列表,即:

{"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}]}

List with 2 address-es , 1 has id 26 and another one 6. Json Path $.address.id 应该返回这两个id.我刚刚看到了采样器源代码,无法获得计数,但是您可以通过向示例中添加另一个后处理器来实现,即 BSF Sampler 并添加此代码:

List with 2 address-es , 1 has id 26 and another one 6. The Json Path $.address.id should return both of these ids. I just saw sampler source code and it's not possible to get the count however you can do it by adding another post processor to your sample i.e BSF Sampler and by adding this code :

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

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

Where ${add} is any variable where you stored results of $.address.id.

这篇关于jMeter RegEx JSON 响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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