• jmeter beanshell判断响应的json串,参数的值是否正确;


    本文包含的json格式:{"key1":{"key2":[{"key3","value3"},{}]}},[{},{}];

     题目,如上图;

    采用beanshell postprocessor结合fastjson库的方法;

    先配置环境,我用的是jmeter 5.11,fastjson用到的1.2.68的版本;需要将fastjson.jar包放到jmeter的安装路径的libext库中,并且在jmeter的Test Plan中引用这个jar包;

     BeanShell Sampler构造json响应结果;代码如下;

    return "{"Status Code":200,"ret":{"id":"id","imageList":[{"originUrl":"图片链接1","originWidth":200,"originHeight":300},{"originUrl":"图片链接2","originWidth":300,"originHeight":300}]}}"
    

    BeanShell PostProcess代码如下;

    import com.alibaba.fastjson.JSON;
    import com.alibaba.fastjson.JSONArray; 
    import com.alibaba.fastjson.JSONObject;
    String rs = prev.getResponseDataAsString();//获取响应结果
    
    JSONObject aa = JSON.parseObject(rs);//整个响应结果作为JSON对象;
    
    JSONObject ret=aa.getJSONObject("ret");//获取某一部分对象。即,json串中{}的内容;
    JSONArray imageList=ret.getJSONArray("imageList");//获取列表。即json串中的[]的内容;
    log.info("结果:"+imageList); //不能写成log.info(imageList);这样会报错;需要加字符串;
    
    boolean result;
    for(int i=0;i<imageList.size();i++){	
    	String x=imageList.getJSONObject(i).getString("originHeight");
    	String y=imageList.getJSONObject(i).getString("originWidth");	
    	log.info(x+" "+y);//查看结果;
    	if(!(x.equals("300")) || !(y.equals("200"))){//注意整数需要使用"括起来,作为字符串;
    		log.info("第"+(i+1)+"项服务器返回结果不正确");
    		result=true;		
    	}
    }
    if(!result){
    	log.info("服务器返回结果正确");
    }
    

     

     

    com.alibaba的fastjson简介

    fastjson是一个Java语言编写的高性能功能完善的JSON库。

    常用api;

    1.String JSON.toJSONString(Object object) //将对象序列化成json字符串;//我认为String代表类型;可以使用String类型的变量去接收这个值;

    2.JSONObject JSON.parseObject(String text) //将json字符串反序列化成JSON对象;

    3.JSONArray JSONObject.getJSONArray(String key) //根据key得到json中的json数组;

    4.JSONObject JSONArray.getJSONObject(int index) //根据下标拿到json数组的json对象;

     JSONObject JSONObject.getJSONObject(String key);

    5.String JSONObject.getString(String key) //根据key拿到json的字符串值;

    6.int JSONObject.getIntValue(String key) //根据key拿到json的int值;

    7.boolean JSONObject.getBooleanValue(String key) //根据key拿到json的boolean值;

    参考:https://www.cnblogs.com/qiaoyeye/p/7730288.html

     下面继续来提取响应json串的DEVICE_ID参数值;

    响应结果如下图;//因为中途遇到报错,各种百度,尝试各种方法,将fastjson放置在了jmeter目录的lib下了(D:apache-jmeter-5.1.1libfastjson-1.2.68.jar),最终发现是Object单词写错了;哎,都是粗心的错;

    {
        "data": {
            "records": [
                {
                    "DEVICE_TYPE": 194,
                    "DEVICE_STATUS": 0,
                    "ORG_NAME": "**省",
                    "LONGITUDE": "0.000000",
                    "DEVICE_NAME": "测试1",
                    "DEVICE_ADDR": "",
                    "DEVICE_ID_INT": "13",
                    "HAVE_VIDEO": "true",
                    "OWNER": "-1",
                    "ORIGINAL_DEVICE_ID": "44000000011325******",
                    "PLATFORM_ID": "44000000052005******",
                    "ORG_CODE": "44",
                    "TYPE": "1",
                    "DEVICE_ID": "44000000011325******",
                    "LATITUDE": "0.000000"
                },
                {
                    "DEVICE_TYPE": 194,
                    "DEVICE_STATUS": 0,
                    "LONGITUDE": "0.000000",
                    "DEVICE_NAME": "测试2",
                    "DEVICE_ADDR": "",
                    "DEVICE_ID_INT": "10",
                    "HAVE_VIDEO": "true",
                    "OWNER": "1",
                    "ORIGINAL_DEVICE_ID": "44010000001320******",
                    "PLATFORM_ID": "44000000052005******",
                    "ORG_CODE": "4401",
                    "TYPE": "1",
                    "DEVICE_ID": "440100000013200******",
                    "LATITUDE": "0.000000"
                },
                {
                    "DEVICE_TYPE": 194,
                    "DEVICE_STATUS": 0,
                    "LONGITUDE": "0.000000",
                    "DEVICE_NAME": "测试3",
                    "DEVICE_ADDR": "",
                    "DEVICE_ID_INT": "11",
                    "HAVE_VIDEO": "true",
                    "OWNER": "1",
                    "ORIGINAL_DEVICE_ID": "44010000001320******",
                    "PLATFORM_ID": "44000000052005******",
                    "ORG_CODE": "4401",
                    "TYPE": "1",
                    "DEVICE_ID": "44010000001320******",
                    "LATITUDE": "0.000000"
                }
            ],
            "count": 3,
            "usetime": 0
        }
    }
    

      方式一、BeanShell Sampler构造响应结果为json的请求;使用BeanShell PostProcessor提取结果;

    return "{"data": {"records": [{"DEVICE_TYPE": 194,"DEVICE_STATUS": 0,"ORG_NAME": "**省","LONGITUDE": "0.000000","DEVICE_NAME": "测试1","DEVICE_ADDR": "","DEVICE_ID_INT": "13","HAVE_VIDEO": "true","OWNER": "-1","ORIGINAL_DEVICE_ID": "44000000011325******","PLATFORM_ID": "44000000052005******","ORG_CODE": "44","TYPE": "1","DEVICE_ID": "44000000011325******","LATITUDE": "0.000000"}, {"DEVICE_TYPE": 194,"DEVICE_STATUS": 0,"LONGITUDE": "0.000000","DEVICE_NAME": "测试2","DEVICE_ADDR": "","DEVICE_ID_INT": "10","HAVE_VIDEO": "true","OWNER": "1","ORIGINAL_DEVICE_ID": "44010000001320******","PLATFORM_ID": "44000000052005******","ORG_CODE": "4401","TYPE": "1","DEVICE_ID": "440100000013200******","LATITUDE": "0.000000"}, {"DEVICE_TYPE": 194,"DEVICE_STATUS": 0,"LONGITUDE": "0.000000","DEVICE_NAME": "测试3","DEVICE_ADDR": "","DEVICE_ID_INT": "11","HAVE_VIDEO": "true","OWNER": "1","ORIGINAL_DEVICE_ID": "44010000001320******","PLATFORM_ID": "44000000052005******","ORG_CODE": "4401","TYPE": "1","DEVICE_ID": "44010000001320******","LATITUDE": "0.000000"}],"count": 3,"usetime": 0}}"
    

      

     

    //从返回的json消息中提取所有的设备ID号(DEVICE_ID);
    import com.alibaba.fastjson.JSON;
    import com.alibaba.fastjson.JSONArray;
    import com.alibaba.fastjson.JSONObject;
    String text=prev.getResponseDataAsString();
    JSONObject a=JSON.parseObject(text);//将JSON字符串反序列化成JSON对象;
    log.info("json结果"+a);
    JSONObject b=a.getJSONObject("data");//根据data得到json中json对象;
    JSONArray c=b.getJSONArray("records");//根据records得到json中的json数组;
    for(int i=0;i<c.size();i++){
    	JSONObject d=c.getJSONObject(i);//根据下标拿到json数组的json对象;
    	String Device_id=d.getString("DEVICE_ID");//根据DEVICE_ID拿到json的字符串值;
    	log.info(Device_id);
    }
    

      

       方式二、BeanShell Sampler一步到位;察看结果树中无json响应结果;

    String text="{"data": {"records": [{"DEVICE_TYPE": 194,"DEVICE_STATUS": 0,"ORG_NAME": "**省","LONGITUDE": "0.000000","DEVICE_NAME": "测试1","DEVICE_ADDR": "","DEVICE_ID_INT": "13","HAVE_VIDEO": "true","OWNER": "-1","ORIGINAL_DEVICE_ID": "44000000011325******","PLATFORM_ID": "44000000052005******","ORG_CODE": "44","TYPE": "1","DEVICE_ID": "44000000011325******","LATITUDE": "0.000000"}, {"DEVICE_TYPE": 194,"DEVICE_STATUS": 0,"LONGITUDE": "0.000000","DEVICE_NAME": "测试2","DEVICE_ADDR": "","DEVICE_ID_INT": "10","HAVE_VIDEO": "true","OWNER": "1","ORIGINAL_DEVICE_ID": "44010000001320******","PLATFORM_ID": "44000000052005******","ORG_CODE": "4401","TYPE": "1","DEVICE_ID": "440100000013200******","LATITUDE": "0.000000"}, {"DEVICE_TYPE": 194,"DEVICE_STATUS": 0,"LONGITUDE": "0.000000","DEVICE_NAME": "测试3","DEVICE_ADDR": "","DEVICE_ID_INT": "11","HAVE_VIDEO": "true","OWNER": "1","ORIGINAL_DEVICE_ID": "44010000001320******","PLATFORM_ID": "44000000052005******","ORG_CODE": "4401","TYPE": "1","DEVICE_ID": "44010000001320******","LATITUDE": "0.000000"}],"count": 3,"usetime": 0}}";
    
    //从返回的json消息中提取所有的设备ID号(DEVICE_ID);
    import com.alibaba.fastjson.JSON;
    import com.alibaba.fastjson.JSONArray;
    import com.alibaba.fastjson.JSONObject;
    //String text=prev.getResponseDataAsString();
    JSONObject a=JSON.parseObject(text);//将JSON字符串反序列化成JSON对象;
    log.info("json结果"+a);
    JSONObject b=a.getJSONObject("data");//根据data得到json中json对象;
    JSONArray c=b.getJSONArray("records");//根据records得到json中的json数组;
    for(int i=0;i<c.size();i++){
    	JSONObject d=c.getJSONObject(i);//根据下标拿到json数组的json对象;
    	String Device_id=d.getString("DEVICE_ID");//根据DEVICE_ID拿到json的字符串值;
    	log.info(Device_id);
    }
    

     

    import com.alibaba.fastjson.*;
    String v=prev.getResponseDataAsString();	
    JSONArray ja=JSON.parseArray(v);
    System.out.println(ja.size());
    

      

     

    越努力,越幸运!!! good good study,day day up!!!
  • 相关阅读:
    online ddl与pt-osc详解
    几个重点问题回顾
    死锁及常见死锁模型
    InnoDB中锁的算法(3)
    一个幻读模型引出的记录可见性判断
    jupyter notebook的使用
    l线程池抓取lianjia
    lagou数据爬取
    爬虫代理的设置
    linux如何安装和启动mongdb
  • 原文地址:https://www.cnblogs.com/canglongdao/p/12843486.html
Copyright © 2020-2023  润新知