zoukankan      html  css  js  c++  java
  • 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包,放置libext下,版本号自选

    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}");

  • 相关阅读:
    Unity场景加载完成回调
    UnityShader 一些算法总结
    Unity ugui 的 Button 组件的 点击、按下、抬起等按钮事件(eventTrigger)
    Unity 鼠标拖拽旋转物体
    JVM
    JVM
    JVM
    JVM
    JVM
    JVM
  • 原文地址:https://www.cnblogs.com/chushujin/p/11993722.html
Copyright © 2011-2022 走看看