Jmeter对Json数据的三种处理方式

现在的接口测试中,大多数请求响应的报文格式都为json格式。那么在Jmeter接口测试中如何对json格式的数据进行处理呢,本文整理了三种常见的方式。

json报文

{
    "accidentNo": "Acc_942900",
    "companyId": "${companyID_83}",
    "companyCode": "${companyCode_83}",
    "account": "${account_83}",
    "claimUniqueId": "claim_895073",
    "interfaceCode": "ClaimPush",
    "message": "83 dummy sampler success",
    "resultCode": "000",
    "auditReport": {
        "auditRuleTriggers": [{
            "actualValue": "    报案次数:\n            2\n",
            "auditScore": 20,
            "itemInfoList": [],
            "itemName": "报案人姓名:王俊",
            "redLineType": "00",
            "ruleName": "报案人在多张保单中出现",
            "ruleNo": "0102010024",
            "ruleType": "02",
            "ruleValue": "    报案次数:\n        1-9999\n"
        },
        {
            "actualValue": "\t\t\t\t定型的具体车型配置:523LI 2011款 2.5L 两驱 手自一体 豪华版\n",
            "auditScore": 30,
            "itemInfoList": [],
            "itemName": "",
            "redLineType": "00",
            "ruleName": "车辆定型的车型配置与承保的车型配置不一致(车型配置名称)",
            "ruleNo": "0102020008",
            "ruleType": "02",
            "ruleValue": "        承保的车型配置:嘉年华 两厢 2009款 1.3L 手动 经典版\n"
        },
        {
            "actualValue": "",
            "auditScore": 25,
            "itemInfoList": [],
            "itemName": "修理厂名称:安徽SC修理厂",
            "redLineType": "00",
            "ruleName": "重点关注修理厂",
            "ruleNo": "0102030001",
            "ruleType": "02",
            "ruleValue": ""
        },
        {
            "actualValue": "            5\n",
            "auditScore": 10,
            "itemInfoList": [],
            "itemName": "",
            "redLineType": "00",
            "ruleName": "碰撞点选择过多",
            "ruleNo": "0102010042",
            "ruleType": "02",
            "ruleValue": "        1\n"
        },
        {
            "actualValue": "",
            "auditScore": 10,
            "itemInfoList": [],
            "itemName": "",
            "redLineType": "00",
            "ruleName": "非第一现场查勘",
            "ruleNo": "0102010014",
            "ruleType": "02",
            "ruleValue": ""
        },
        {
            "actualValue": "",
            "auditScore": 20,
            "itemInfoList": [{
                "claimItemUniqueId": null,
                "itemName": "右前翼子板后内衬板",
                "itemType": "02",
                "materialFeeAfterDiscount": null,
                "paintFeeAfterDiscount": 0.5,
                "partFeeAfterDiscount": null,
                "propAmount": null,
                "propSecondType": null,
                "propType": null,
                "quantity": null,
                "removeLaborFeeDiscount": 12.5,
                "repairLaborFeeDiscount": 1,
                "standardPartId": "162",
                "unitPrice": null
            }],
            "itemName": "右前翼子板后内衬板\n",
            "redLineType": "00",
            "ruleName": "不建议外修的配件进行了外修操作",
            "ruleNo": "0103010018",
            "ruleType": "03",
            "ruleValue": ""
        }]
    }
}

(1)使用正则表达式提取
在对应的请求下添加正则表达式提取器(后置处理器-->正则表达式提取器)


Jmeter对Json数据的三种处理方式_第1张图片
image.png

可以通过debug sampler查看


Jmeter对Json数据的三种处理方式_第2张图片
image.png

(2)使用 JMeter 的后置处理器JSON Extractor获取json数据中特定位置的数据
在对应的请求下添加JSON Extractor(后置处理器-->JSON Extractor)


Jmeter对Json数据的三种处理方式_第3张图片
image.png

可以通过察看结果树,JSON Path Tester验证结果是否正确


Jmeter对Json数据的三种处理方式_第4张图片
image.png

(3)使用JMeter的后置处理器BeanShell Post Processor来处理Json数据
需要将fastjson.jar包拷贝到 jmeter lib 文件夹下后重启 jmeter
在在对应的请求下添加后置处理器-->BeanShell PostProcessor


Jmeter对Json数据的三种处理方式_第5张图片
image.png

代码:

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

String json=prev.getResponseDataAsString();
JSONObject jso = JSON.parseObject(json);
JSONObject data = jso.getJSONObject("auditReport");
JSONArray array = data.getJSONArray("auditRuleTriggers");
//vars.put("ruleName",array.getJSONObject(1).getString("ruleName"));
log.info("-----------------------------------------------------------------------------"+data);

int length=0;
for(Object o : array){
    String ruleName = o.getString("ruleName");
    log.info(ruleName);
    length++;
}
vars.put("length", Integer.toString(length));
log.info("-----------------------------------------------------------------------------"+length);

你可能感兴趣的:(Jmeter对Json数据的三种处理方式)