由Beanshell在jmeter中从response rest服务获取第一个json对象

由Beanshell在jmeter中从response rest服务获取第一个json对象,jmeter,jmeter-plugins,jmeter-5.0,Jmeter,Jmeter Plugins,Jmeter 5.0,我从我的web服务获得此JSON响应: [ { "type": "022", "sendDate": "2020-11-09 12:43:07", "message": "test1", "id": 8035, }, { "notificatio

我从我的web服务获得此JSON响应:

[
    {
        "type": "022",
        "sendDate": "2020-11-09 12:43:07",
        "message": "test1",
        "id": 8035,
    },
    {
        "notificationType": "023",
        "sendDate": "2020-11-09 11:40:02",
        "message": "test2 ",
        "id": 8034,
    },...
]
现在,通过JMeter中的Beanshell,我想将第一个id传递到新请求中,因此在JSR223后处理器中,我编写了如下简单打印:

import com.eclipsesource.json.*;
String jsonString = prev.getResponseDataAsString();
log.info("value=" + jsonString);
JsonArray inbox =  Json.parse(jsonString).asArray();
//String id_BSH = ((JSONObject) storeArray.get(0)).getAsString("id");
//vars.put("id_BSH", pickup);
但我有一个错误:

2020-11-10 17:59:55,245 ERROR o.a.j.e.JSR223PostProcessor: Problem in JSR223 script, JSR223 PostProcessor
javax.script.ScriptException: Sourced file: inline evaluation of: ``import com.eclipsesource.json.*; String jsonString = prev.getResponseDataAsStrin . . . '' : Typed variable declaration : Class: JsonArray not found in namespace : at Line: 4 : in file: inline evaluation of: ``import com.eclipsesource.json.*; String jsonString = prev.getResponseDataAsStrin . . . '' : JsonArray 
 in inline evaluation of: ``import com.eclipsesource.json.*; String jsonString = prev.getResponseDataAsStrin . . . '' at line number 4
    at bsh.engine.BshScriptEngine.evalSource(BshScriptEngine.java:93) ~[bsh-2.0b6.jar:2.0b6 2016-02-05 05:16:19]
    at bsh.engine.BshScriptEngine.eval(BshScriptEngine.java:46) ~[bsh-2.0b6.jar:2.0b6 2016-02-05 05:16:19]
    at javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:233) ~[?:1.8.0_202]
    at org.apache.jmeter.util.JSR223TestElement.processFileOrScript(JSR223TestElement.java:224) ~[ApacheJMeter_core.jar:5.3]
    at org.apache.jmeter.extractor.JSR223PostProcessor.process(JSR223PostProcessor.java:45) [ApacheJMeter_components.jar:5.3]
    at org.apache.jmeter.threads.JMeterThread.runPostProcessors(JMeterThread.java:940) [ApacheJMeter_core.jar:5.3]
    at org.apache.jmeter.threads.JMeterThread.executeSamplePackage(JMeterThread.java:572) [ApacheJMeter_core.jar:5.3]
    at org.apache.jmeter.threads.JMeterThread.processSampler(JMeterThread.java:489) [ApacheJMeter_core.jar:5.3]
    at org.apache.jmeter.threads.JMeterThread.run(JMeterThread.java:256) [ApacheJMeter_core.jar:5.3]
    at java.lang.Thread.run(Thread.java:748) [?:1.8.0_202]
我听说,您可以使用Groovy的类将第一个ID存储到一个变量中,并使用以下一行代码:

vars.put("id_BSH", new groovy.json.JsonSlurper().parse(prev.getResponseData()).get(0).id as String)

更多信息:

您是否将最小json jar添加到JMeter类路径?是的,我将
最小json
添加到lib JMeter@user7294900Jar不在lib folder中我必须放置的文件夹是什么@user7294900
自JMeter 3.1以来,我们建议从BeanShell切换到JSR223测试元素
。我使用的是JSR223@dmitri您使用的是带有BeanShell语言的JSR223测试元素,您应该使用groovy