Jmeter接口自动化-5-提取JSON响应中数组的长度

json响应如下:

{
    "code":0,
    "data":{
        "data":[
            {
                "amount":50000,
                "appointmentInfoState":"00",
                "appointmentState":"",
                "appointmentTime":""
                
            },
            {
                "amount":50000,
                "appointmentInfoState":"00",
                "appointmentState":"",
                "appointmentTime":"",
                "auditTime":"审核耗时15小时 11分钟",
                "createTime":"2019-12-05 18:24:56"
                
            },
            {
                "amount":50000,
                "appointmentInfoState":"00",
                "appointmentState":"",
                "appointmentTime":"",
                "auditTime":"审核耗时15小时 54分钟"
            }
            {
                "amount":50000,
                "appointmentInfoState":"00",
                "appointmentState":"",
                "appointmentTime":""
                
            },
            {
                "amount":300000,
                "appointmentInfoState":"00",
                "appointmentState":"",
                "appointmentTime":"",
                "auditTime":"审核耗时209小时 44分钟",
                "createTime":"2019-11-27 15:51:44"
            },
            {
                "amount":300000,
                "appointmentInfoState":"00",
                "appointmentState":"",
                "appointmentTime":"",
                "auditTime":"审核耗时1917小时 0分钟"
            },
            {
                "amount":300000,
                "appointmentInfoState":"00",
                "appointmentState":"",
                "appointmentTime":"",
                "auditTime":"审核耗时1917小时 56分钟",
                "createTime":"2019-09-17 11:39:22"
            },
            {
                "amount":300000,
                "appointmentInfoState":"00",
                "appointmentState":"",
                "appointmentTime":"",
                "auditTime":"审核耗时1984小时 54分钟",
                "createTime":"2019-09-14 16:41:58"
            },
            {
                "amount":300000,
                "appointmentInfoState":"00",
                "appointmentState":"",
                "appointmentTime":"",
                "auditTime":"审核耗时1989小时 53分钟",
                "createTime":"2019-09-14 11:42:29"
            },
            {
                "amount":300000,
                "appointmentInfoState":"00",
                "appointmentState":"",
                "appointmentTime":"",
                "auditTime":"审核耗时2369小时 45分钟",
                "createTime":"2019-08-29 15:50:46"
            }
        ]
    },
    "mask":"c7d2f67d-a5e8-45a3-8f4b-0149c4a7e434",
    "msg":"success",
    "timestamp":1575596175
}

取出data对象下data数据的长度

1、首先导入alibaba的fastjson-1.2.59.jar包,放置lib\ext下,版本号自选

2、在接口之后添加BeanShell PostProcessor工具

编写代码如下:

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


String jsonContent = prev.getResponseDataAsString();

JSONObject response = JSON.parseObject(jsonContent);
JSONArray dataList = response.getJSONObject("data").getJSONArray("data");
int length = dataList.size();

vars.put("m_length",length.toString());

注意:一下这样写是错误的

vars.put("m_length",length);

报错信息如下:

Error in method invocation: Method put( java.lang.String, int ) not found in class'org.apache.jmeter.threads.JMeterVariables'

没有找到put( Java.lang.String, int )这个方法。此处put的value应该是String

所以需要将values转换为String类型

 

最后正确代码如下:

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


String jsonContent = prev.getResponseDataAsString();

JSONObject response = JSON.parseObject(jsonContent);
JSONArray dataList = response.getJSONObject("data").getJSONArray("data");
int length = dataList.size();

vars.put("m_length",length.toString());

log.info("m_length=${m_length}");

 

posted @ 2019-12-06 10:21  旅行没有终点  阅读(1867)  评论(0编辑  收藏  举报