我们如何在 Jmeter 中使用 Beanshell 验证 Json 响应?

How can we Verify Json response using Beanshell in Jmeter?

I am getting Json as a response. Instead of using multiple jsonPath_assertion I want to check it in single beanshell. Each time I am getting Response with different values.

我建议改用 JSR223 Assertion and Groovy language

Groovy 内置了 JSON support so you will be able to use JsonSlurper class 来解析像这样的响应:

def json = new groovy.json.JsonSlurper.parseText(prev.getResponseDataAsString())
// do what you need here

此外 Groovy 性能比 Beanshell 好得多,recommended to use it for scripting in JMeter. See Groovy Is the New Black 了解更多信息。

You can use beanshell assertion.It will help you a lot. prev.getResponseDataAsString() will return your json response as a string. I hope you would be using Jsonpath online evaluator for evaluating json responses. Suppose if my json online evaluator code looks like this $.items[2].CM_SEQNUMBER In beanshell assertion i have used

 JsonObject jsonobj = JsonObject.readFrom(jsonString);
    JsonArray jsonarr = jsonobj.get("items").asArray();
    String pickupToCheck=jsonarr.get(2).asObject().get("CM_SEQNUMBER").asString();

Similarly you can verify your JSON data without using multiple JSONPath Extractor using single beanshell assertion.