我有一个示例JSON响应如下:
{
"id": 37,
"merchant_id": "39",
"title": "Parker Pens",
"subtitle": null,
"price": 1000,
"description": null,
"images": [],
"image_thumbs": [],
"options": [{
"code": "color",
"label": "Color",
"extra_info": "",
"values": [{
"label": "Red",
"value": "8"
}, {
"label": "Yellow",
"value": "9"
}, {
"label": "Pink",
"value": "10"
}]
}, {
"code": "size",
"label": "Size",
"extra_info": "",
"values": [{
"label": "Small",
"value": "4"
}, {
"label": "Medium",
"value": "5"
}, {
"label": "Large",
"value": "6"
}]
}],
"options_available": [{
"combination": [{
"code": "color",
"value": "Red"
}, {
"code": "size",
"value": "Small"
}]
}, {
"combination": [{
"code": "color",
"value": "Red"
}, {
"code": "size",
"value": "Medium"
}]
}, {
"combination": [{
"code": "color",
"value": "Red"
}, {
"code": "size",
"value": "Large"
}]
}, {
"combination": [{
"code": "color",
"value": "Yellow"
}, {
"code": "size",
"value": "Small"
}]
}, {
"combination": [{
"code": "color",
"value": "Yellow"
}, {
"code": "size",
"value": "Medium"
}]
}, {
"combination": [{
"code": "color",
"value": "Yellow"
}, {
"code": "size",
"value": "Large"
}]
}, {
"combination": [{
"code": "color",
"value": "Pink"
}, {
"code": "size",
"value": "Small"
}]
}, {
"combination": [{
"code": "color",
"value": "Pink"
}, {
"code": "size",
"value": "Medium"
}]
}, {
"combination": [{
"code": "color",
"value": "Pink"
}, {
"code": "size",
"value": "Large"
}]
}],
"custom_options": []
}
我将我的采样器作为
import org.json.JSONObject;
import org.json.JSONArray;
String response= prev.getResponseDataAsString();
JSONObject jsonObject = new JSONObject(response);
JSONArray jsonArray = jsonObject.getJSONArray("options");
Integer count= jsonArray.length();
vars.put('counts', count);
但是在运行脚本时我遇到了错误:
No signature of method: org.apache.jmeter.threads.JMeterVariables.put() is applicable for argument types: (java.lang.String, java.lang.Integer)
除了值之外(Counts = 2)。我的目的是在“选项”键中获取数组的计数(参见上面的响应)
答案 0 :(得分:2)
vars.put
不支持与常规String
不同的值,并且您尝试设置Integer
值。
通过简单转换轻松解决问题:
vars.put("counts", Integer.toString(count));
另一个选项是使用vars.putObject
保存对象vars.putObject("counts", count);
根据JMeter best practices移动到JSR223 Sampler:
从JMeter 3.1开始,我们建议从BeanShell切换到JSR223测试元素
答案 1 :(得分:1)
转到JSR223 PostProcessor和Groovy语言:
你的7行Beanshell代码的Groovy相当于:
vars.put('counts',new groovy.json.JsonSlurper().parse(prev.getResponseData()).options.size() as String)