使用Bean Shell后处理器提取JSON响应

Ash*_*ish 3 json jmeter beanshell

我试图使用Beanshell后处理器提取JSON数组的一个变量的值,但我没有在日志中得到任何响应

我的JSON有点像:

"store":
:   [
:   :   {
:   :   :   "storeId":12345,
:   :   :   "storeName":"ABC",
:   :   :   "storeAddress":"DEFGHIJKL",
:   :   :   "storeMinOrderAmount":100,
:   :   :   "mobile":"+911234567890",
:   :   :   "mobileSecondary":null,
:   :   :   "city":"Somewhere",
:   :   :   "pincode":123456,
:   :   :   "country":"India",
:   :   :   "email":"ptrm@company.com",
:   :   :   "pickup":true,
:   :   :   "delivery":false,
:   :   :   "storeSplashPath":null,
:   :   :   "storeSplashType":null,
:   :   :   "distance":"0.10"
:   :   },
Run Code Online (Sandbox Code Playgroud)

而我的Beanshell Post处理器是:

import org.apache.commons.lang3.StringUtils;
import org.json.JSONArray;
import org.json.JSONObject;
import com.eclipsesource.json.*;

print("*******************");

//Get Store total count
int totalStoreNumber = StringUtils.countMatches(new String(data), "storeId");
print("Total Number of Stores are: " + totalStoreNumber);

if (totalStoreNumber > 0) {
 //Check for Fulfilment type is "Pickup"
String jsonString = prev.getResponseDataAsString();
JsonObject store = JsonObject.readFrom(jsonString);
JsonArray store = store.get("store").asArray();
String pickup = store.get(1).asObject().get("pickup").asString();
vars.put("fulfilmentType_BSH", pickup);
print("Is Pickup allowed: " + pickup);
}
else {
 print("No Stores Nearby");
}
Run Code Online (Sandbox Code Playgroud)

我不知道我哪里错了.我已阅读相关查询,但无法做到这一点.任何的想法?

Dmi*_*i T 7

首先,为什么不使用JSON Path PostProcessor呢?您可以使用单个简单的JSON Path表达式完全相同,如:

$.store[0].pickup
Run Code Online (Sandbox Code Playgroud)

如果由于任何原因你还需要在Beanshell中做到这一点我有一些想法:

  1. 这绝对是错误.您不能在Beanshell脚本中声明具有相同名称的2个变量

    JsonObject store = JsonObject.readFrom(jsonString);
    JsonArray store = store.get("store").asArray(); 
    //        ^^^^^  ka-boom!
    
    Run Code Online (Sandbox Code Playgroud)
  2. 可能的问题.如果响应中只有1个存储,则为IndexOutOfBoundsException.在Beanshell中,集合从零开始,第一个元素的索引为0.

    String pickup = store.get(1).asObject().get("pickup").asString();
    //                        ^ ka-boom! 
    
    Run Code Online (Sandbox Code Playgroud)
  3. 另一个可能的问题可能是你的进口,以防万一

    import org.json.JSONArray;
    import org.json.JSONObject;
    import com.eclipsesource.json.*;
    
    Run Code Online (Sandbox Code Playgroud)

    您是否已将相关的jar添加到JMeter Classpath并在此之后重新启动JMeter?你确定你正确使用方法吗?

以下是使用json-smart重新实现的代码,它带有JMeter 3.0(您不需要任何其他jar)

import net.minidev.json.JSONArray;
import net.minidev.json.JSONObject;
import net.minidev.json.parser.JSONParser;
import org.apache.commons.lang.StringUtils;

//Get Store total count
int totalStoreNumber = StringUtils.countMatches(new String(data), "storeId");
log.info("Total Number of Stores are: " + totalStoreNumber);

if (totalStoreNumber > 0) {
    //Check for Fulfilment type is "Pickup"
    String jsonString = new String(data);
    JSONParser parser = new JSONParser(JSONParser.MODE_JSON_SIMPLE);
    JSONObject store = (JSONObject) parser.parse(data);
    JSONArray storeArray = (JSONArray) store.get("store");
    String pickup = ((JSONObject) storeArray.get(0)).getAsString("pickup");
    vars.put("fulfilmentType_BSH", pickup);
    log.info("Is Pickup allowed: " + pickup);
} else {
    log.info("No Stores Nearby");
}
Run Code Online (Sandbox Code Playgroud)

以及其工作的证据

Beanshell JSON演示

有关在JMeter测试中使用Beanshell脚本的详细信息,请参见如何使用BeanShell:JMeter的"收藏内置组件"指南