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

查看:92
本文介绍了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>

这很快就解决了.谢谢!

Which made the fix quickly. Thanks!

推荐答案

我建议您看看:

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

本节(JSON utils(JSON路径声明,JSON路径提取器,JSON格式化程序))特别针对此情况.这些是我公司开发的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).

从评论中的问题更新:

如果要获取值列表,即:

If you were to have list of values i.e. :

{"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个地址-es的列表,其中1个具有ID 26,另一个具有6.Json路径$.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天全站免登陆