如何在Jmeter中提取响应数据JSON并将变量保存到CSV文件

时间:2018-08-01 10:07:39

标签: jmeter

我正在尝试从响应数据中提取键“ count”,并将其值(int)写入CSV文件。

我已经使用了BeanShell PostProcessor。

下面是我使用的脚本:

count = vars.get("count");

f = new FileOutputStream("file path", true);
p = new PrintStream(f); 
this.interpreter.setOut(p); 
print(count);
f.close();

还有JSON示例,我正在尝试提取:

{"meta":{"message":"","is_error":false,"count":295,"next":"123","status":1000,"previous":""}

这样做,“ null”被打印在CSV文件中。

请让我们知道我在这里想念什么?

应该是295个以CSV文件格式打印。

2 个答案:

答案 0 :(得分:0)

检查提取器是否正在获取正确的值。在下面,我使用了相同的代码,并使用了正则表达式提取器从json中获取值。一切正常。

希望有帮助。

enter image description here

Reg Ex更新

正则表达式配置;- enter image description here

下面是测试正则表达式。您始终可以使用RegEx Tester在视图结果树中测试正则表达式:-

enter image description here

答案 1 :(得分:0)

Since JMeter 3.1 users are strongly advised to switch to JSR223 Test Elements and Groovy language主要是因为Groovy has much better performance comparing to Beanshell

因此,我建议使用JSR223 PostProcessor以便提取“计数”值并将其写入“文件”中,相关代码如下:

new File('file path') << new groovy.json.JsonSlurper().parse(prev.getResponseData()).meta.count

更多详细信息:

相关问题