Jmeter - JsonObject&JsonArray的使用
需求背景:重复请求一个接口,提取返回的产品型号,按序打印。当返回的数据有产品型号时才能用JSON提取器提取到结果,当返回的数据没有产品型号时想输出"-",因此采用BeanShell后置处理器
JsonObject&JsonArray 使用之前需要导包
jar包下载地址:https://search.maven.org/
JSON格式如下:
{
"success": true,
"code": 0,
"message": null,
"body": {
"items": [
{
"isNonStandard": false,
"productId": 32020,
"skuNumber": "H37060",
"manufacturerPartNumber": "CX-444",
"attributesJson": {
"种类": "距离设定反射型",
"输出": "NPN",
"检测距离": "15~100mm[距离设定100mm时](距离设定范围:20~100mm)",
"输出动作": "检测时ON/非检测时ON通过切换开关选择",
"电缆": "2m",
"描述": ""
},
"manufacturer": {
"id": 80,
"name": "松下 Panasonic"
},
"isActive": true,
"series": "CX-400",
"orderCode": "",
"isBlackCardProduct": true,
"clearance": true,
"outofStock": false,
"packQty": 1,
"salesPackUom": "个",
"packUom": "个",
....
BealShell后置处理器:
import org.json.*;
String pageIndex = vars.get("pageIndex");
int pageIndexInt = Integer.parseInt(pageIndex);
pageIndexInt++;
//log.info("====================="+ pageIndexInt+"");
vars.put("pageIndex", pageIndexInt+"");
String res = prev.getResponseDataAsString();
//将返回JSON串构建为JSON对象
JSONObject jsonRes = new JSONObject(res);
//获得body JSON 对象,等同于$.body
JSONObject bodyObj = jsonRes.get("body");
//item是一个数组,因此需使用getJSONArray
JSONArray items = bodyObj.getJSONArray("items");
log.info("page:" +${pageIndex} + "============产品名称============");
for (JSONObject item: items) {
JSONObject attributesJsonObject = item.get("attributesJson");
if (null != attributesJsonObject) {
if (attributesJsonObject.has("产品名称")) { //判断当前对象是否有产品名称的属性
String name = attributesJsonObject.getString("产品名称");
if (null != name) {
log.info(name);
}
} else {
log.info("-");
}
}
}
//编码
log.info("page:" +${pageIndex} +"============型号============");
for(int x=1; x<=12;x++){
skuNumber = vars.get("skuNumber_" + x);
log.info(skuNumber);
}
//系列名称
log.info("page:" +${pageIndex} +"============系列名称============");
for(int x=1; x<=12;x++){
serialName2 = vars.get("serialName2_" + x);
log.info(serialName2);
}
//最小起购量
log.info("page:" +${pageIndex} +"============最小起购量============");
for(int x=1; x<=12;x++){
packQty = vars.get("packQty_" + x);
salesPackUom = vars.get("salesPackUom_" + x);
log.info(packQty+salesPackUom);
}
//价格
log.info("page:" +${pageIndex} +"============价格============");
for(int x=1; x<=12; x++){
price = vars.get("price2_" + x);
log.info(price);
}
结果:
2022-10-25 19:21:19,338 INFO o.a.j.u.BeanShellTestElement: page:4============产品名称============
2022-10-25 19:21:19,338 INFO o.a.j.u.BeanShellTestElement: A6 伺服电机
2022-10-25 19:21:19,339 INFO o.a.j.u.BeanShellTestElement: -
2022-10-25 19:21:19,339 INFO o.a.j.u.BeanShellTestElement: -
2022-10-25 19:21:19,339 INFO o.a.j.u.BeanShellTestElement: -
2022-10-25 19:21:19,339 INFO o.a.j.u.BeanShellTestElement: -
2022-10-25 19:21:19,339 INFO o.a.j.u.BeanShellTestElement: -
2022-10-25 19:21:19,339 INFO o.a.j.u.BeanShellTestElement: -
2022-10-25 19:21:19,339 INFO o.a.j.u.BeanShellTestElement: 光纤头
2022-10-25 19:21:19,339 INFO o.a.j.u.BeanShellTestElement: CPU单元
2022-10-25 19:21:19,339 INFO o.a.j.u.BeanShellTestElement: 电池
2022-10-25 19:21:19,339 INFO o.a.j.u.BeanShellTestElement: FP-X功能插件
2022-10-25 19:21:19,339 INFO o.a.j.u.BeanShellTestElement: -
遇到的坑:
1.BeanShell 中使用data 作为变量的名称,导致报错:Variable assignment: data: Can't assign byte [] to java.lang.String
解决:data是Jmeter内置的变量名称,因此不能使用换个变量名即可
2.JSON串中包含有\n 表示换行,在JS中使用json.parse()解析数据会报错:unterminated string literal
解决:
JSON.parse(String.raw`{"count" : 1, "stack" : "sometext\n"}`)
{count: 1, stack: 'sometext\n'}
参考了一下写的较好的文章:
http://t.zoukankan.com/nieliangcai-p-10535194.html
https://blog.csdn.net/henry_lin_wind/article/details/83345555
https://blog.csdn.net/qq_48607414/article/details/125741047
JSON 中处理换行:https://segmentfault.com/q/1010000042401230
本文来自博客园,作者:chuangzhou,转载请注明原文链接:https://www.cnblogs.com/czzz/p/16826015.html