使用Bean Shell后处理器提取JSON响应 [英] Extracting JSON Response using Bean Shell Postprocessor
问题描述
我正在尝试使用Beanshell后处理器提取JSON数组的一个变量的值,但在日志中没有任何响应
I am trying to extract the value of one variable of a JSON array using Beanshell postprocessor but I am not getting any response in log
我的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"
: : },
我的Beanshell后处理器是:
And my Beanshell Post Processor is:
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");
}
我不知道我要去哪里.我已经阅读了相关的查询,但是无法正确执行此操作. 有想法吗?
I don't know where I am going wrong. I had read the related queries but couldn't get this right. Any Idea?
推荐答案
首先,为什么不使用 JSON路径后处理程序吗?您可以使用单个简单的 JSON Path 表达式获得完全相同的结果,例如:
First of all, why don't you use JSON Path PostProcessor for it? You can get absolutely the same using single simple JSON Path expression like:
$.store[0].pickup
如果出于任何原因您仍然需要在Beanshell中进行操作,我有一些想法:
If for any reason you still need to do it in Beanshell I have some ideas:
-
这绝对是错误.您无法在Beanshell脚本中声明两个具有相同名称的变量
This is definitely the error. You cannot declare 2 variables with the same name in Beanshell script
JsonObject store = JsonObject.readFrom(jsonString);
JsonArray store = store.get("store").asArray();
// ^^^^^ ka-boom!
可能是问题. IndexOutOfBoundsException 如果只有1家商店响应.在Beanshell集合中,基数从零开始,第一个元素的索引为0.
Possible problem. IndexOutOfBoundsException if there will be only 1 store in the response. In Beanshell collections are zero-based, 1st element will have index of 0.
String pickup = store.get(1).asObject().get("pickup").asString();
// ^ ka-boom!
另一个可能的问题是您的进口商品,以防万一
Another possible problem could be regarding your imports, just in case
import org.json.JSONArray;
import org.json.JSONObject;
import com.eclipsesource.json.*;
您是否已将相关的jar添加到 JMeter Classpath 中,之后重启JMeter吗?您确定使用正确的方法吗?
Have you added the relevant jars to JMeter Classpath and have you restarted JMeter after this? Are you sure you're using methods correctly?
这是您使用JMeter 3.0随附的 json-smart 重新实现的代码(您不需要其他罐子
Here is your code re-implemented using json-smart which comes with JMeter 3.0 (you don't need any other jars)
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");
}
及其工作证据
请参见如何使用BeanShell:JMeter最喜欢的内置组件指南,以获取有关在JMeter测试中使用Beanshell脚本的更多信息
See How to Use BeanShell: JMeter's Favorite Built-in Component guide for more information on using Beanshell scripting in your JMeter tests
这篇关于使用Bean Shell后处理器提取JSON响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!