我在J抄JSR223采样器中运行下面的java代码时出错
import com.jayway.jsonpath.Configuration;
import com.jayway.jsonpath.JsonPath;
import com.jayway.jsonpath.Option;
import net.minidev.json.JSONArray;
String json = "{\"store\":{\"book\":[{\"category\":\"reference\",\"author\":\"Nigel Rees\",\"title\":\"Sayings of the Century\",\"price\":8.95},{\"category\":\"fiction\",\"author\":\"Evelyn Waugh\",\"title\":\"Sword of Honour\",\"price\":12.99},{\"category\":\"fiction\",\"author\":\"Herman Melville\",\"title\":\"Moby Dick\",\"isbn\":\"0-553-21311-3\",\"price\":8.99},{\"category\":\"fiction\",\"author\":\"J. R. R. Tolkien\",\"title\":\"The Lord of the Rings\",\"isbn\":\"0-395-19395-8\",\"price\":22.99}],\"bicycle\":{\"color\":\"red\",\"price\":19.95}},\"expensive\":10}";
String jsonPath = "$..book[?(@.author == 'Nigel Rees')].title";
Configuration config = Configuration.defaultConfiguration().addOptions(Option.DEFAULT_PATH_LEAF_TO_NULL);
JSONArray authorsArr = JsonPath.using(config).parse(json).read(jsonPath);
System.out.println(authorsArr.get(0).toString());json 2.4.0.jar是在..\lib\ext下添加的,并假设它是自动加载到Jmeter上的。
上面的代码在IDE中签入并运行良好。
JSR223错误
2021-02-02 17:19:10,580 ERROR o.a.j.p.j.s.JSR223Sampler: Problem in JSR223 script JSR223 Sampler, message: javax.script.ScriptException: Sourced file: inline evaluation of: ``import com.jayway.jsonpath.Configuration; import com.jayway.jsonpath.JsonPath; i . . . '' : Typed variable declaration : Error in method invocation: Method addOptions( com.jayway.jsonpath.Option ) not found in class'com.jayway.jsonpath.Configuration' : at Line: 11 : in file: inline evaluation of: ``import com.jayway.jsonpath.Configuration; import com.jayway.jsonpath.JsonPath; i . . . '' : .addOptions ( Option .DEFAULT_PATH_LEAF_TO_NULL )
in inline evaluation of: ``import com.jayway.jsonpath.Configuration; import com.jayway.jsonpath.JsonPath; i . . . '' at line number 11
javax.script.ScriptException: Sourced file: inline evaluation of: ``import com.jayway.jsonpath.Configuration; import com.jayway.jsonpath.JsonPath; i . . . '' : Typed variable declaration : Error in method invocation: Method addOptions( com.jayway.jsonpath.Option ) not found in class'com.jayway.jsonpath.Configuration' : at Line: 11 : in file: inline evaluation of: ``import com.jayway.jsonpath.Configuration; import com.jayway.jsonpath.JsonPath; i . . . '' : .addOptions ( Option .DEFAULT_PATH_LEAF_TO_NULL )
in inline evaluation of: ``import com.jayway.jsonpath.Configuration; import com.jayway.jsonpath.JsonPath; i . . . '' at line number 11
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(Unknown Source) ~[?:1.8.0_241]
at org.apache.jmeter.util.JSR223TestElement.processFileOrScript(JSR223TestElement.java:223) ~[ApacheJMeter_core.jar:4.0 r1823414]
at org.apache.jmeter.protocol.java.sampler.JSR223Sampler.sample(JSR223Sampler.java:69) [ApacheJMeter_java.jar:4.0 r1823414]
at org.apache.jmeter.threads.JMeterThread.executeSamplePackage(JMeterThread.java:490) [ApacheJMeter_core.jar:4.0 r1823414]
at org.apache.jmeter.threads.JMeterThread.processSampler(JMeterThread.java:416) [ApacheJMeter_core.jar:4.0 r1823414]
at org.apache.jmeter.threads.JMeterThread.run(JMeterThread.java:250) [ApacheJMeter_core.jar:4.0 r1823414]
at java.lang.Thread.run(Unknown Source) [?:1.8.0_241]发布于 2021-02-02 15:38:39
groovy”,如果选择java,这不是真正的贝贝,而是贝贝- is not fully Java-compliant
- has much worse performance comparing to Groovy所以根据JMeter最佳实践,您应该从JMeter 3.1开始切换到JMeter。更多信息:为什么和如何使用它

还请注意,Groovy有内置JSON支持
https://stackoverflow.com/questions/66012571
复制相似问题