jmeter引用json的jar包处理beanshell的json响应

1、测试web项目,在web项目的WEB-INF/lib目录中找到对应的json包,如json-20140107.jar

如:可以在http://download.csdn.net/download/xiaoxiao_renhe/10246640中下载该json的jar包。

2、将该jar包放入到jmeter的lib\ext\目录下,以便jmeter调用引用

3、jmeter脚本引入该json包,如下截图所示:











4、响应的json形式如下:













5、BeanShell断言中该json包的使用:













6、该beanShell断言代码如下:

import org.json.JSONObject;

boolean flag = false;

if(200!=ResponseCode){
    flag = true;
}
if(!ResponseMessage.equals("OK")){
    flag = true;
}
String response = prev.getResponseDataAsString();
JSONObject responseJson = new JSONObject(response);
String msg = responseJson.getString("msg");
if(!msg.equals("用户登录成功")){
    flag = true;
}
String data = responseJson.get("result").toString();
JSONObject strdata = new JSONObject(data);
log.info("----------"+strdata);
String fbpcontent = strdata.getString("userName");

if(flag){
    Failure = true;
    FailureMessage = "断言失败原因!"
}else{
    Failure = false;
    FailureMessage = "测试通过!"    
}

7、响应的json如下(包含json数组)

















8、该beanShell断言代码如下:





















9、源码如下:

import org.json.JSONObject;
import org.json.JSONArray;

String beforeId = vars.get("registerId");
boolean flag = false;

try{
    String response = prev.getResponseDataAsString();
    JSONObject responseJson = new JSONObject(response);
    JSONArray infoArrays = responseJson.getJSONArray("datas");
    int len = infoArrays.length();
    
    int i = 0;
    String tempId = "";
    String tempIsMessage = "";
    for(i=0; i<len; i++){
        JSONObject tempInfo = (JSONObject)infoArrays.getJSONObject(i);
        tempId = tempInfo.getString("id");
        tempIsMessage = tempInfo.getString("isMessage");
        if(beforeId.equals(tempId) && "0".equals(tempIsMessage)){
            flag = true;
            break; //确认当前记录没有发消息
        }
    }//end for

}catch(Exception e){
    e.printStackTrace();
}


if(flag){
    Failure = false;
    FailureMessage = "断言成功,断言是:没发过消息,图标灰色";
}else{
    Failure = true;
    FailureMessage = "断言失败,没找到该记录或已发过消息";    
}

10、json数组中嵌套对象取值方法,json响应如下截图


















11、BeanShell断言如下:















12、源码如下:

import org.json.JSONObject;  
import org.json.JSONArray;  

boolean flag = false;
String tempCode = "";
try{  
	String response = prev.getResponseDataAsString();  
     JSONObject responseJson = new JSONObject(response);  
     JSONArray infoArrays = responseJson.getJSONArray("datas");  
     int len = infoArrays.length();  
     
     int i = 0;
     String itemId = ""; 
     String name = "";
     for(i=0; i<len; i++){  
         JSONObject tempInfo = (JSONObject)infoArrays.getJSONObject(i); 
         JSONObject tempRunVo = (JSONObject)tempInfo.getJSONObject("runSampVo");
         tempCode = tempRunVo.getString("sampCode");
         itemId = tempInfo.getString("id");
         name = tempInfo.getString("name");
         break; //只取第一个项目的id和name
      }//end for  
	vars.put("itemId",itemId);
	vars.put("name",name);
	vars.put("backReson","任务分配已接收单独"+name+"退回");
}catch(Exception e){  
      e.printStackTrace();  
}

String beforeCode = vars.get("code");
if(beforeCode.equals(tempCode)){
	flag = true;
}

if(flag){  
    Failure = false;  
    FailureMessage = "断言成功,断言是:任务分配已接收查询成功";  
}else{  
    Failure = true;  
    FailureMessage = "断言失败,任务分配已接收没有查询到";      
}  


### 回答1: JMeter是一个开源的性能测试工具,它支持多种协议和数据格式的测试。其中,JSON是一种常见的数据格式,因此JMeter针对JSON格式提供了一个jar包——json.jar。 json.jar包包含了一些针对JSON数据的解析和生成工具,可以方便地对JSON数据进行处理。在使用JMeter进行接口测试时,通常需要从接口返回的JSON数据中提取出需要验证的参数,这时就可以使用json.jar中的工具来解析JSON数据,并从中获取所需的参数。 除了解析JSON数据之外,json.jar包还支持将Java对象转换为JSON格式字符串,并且可以很方便地在JMeter脚本中使用。例如,在编写HTTP请求时,可以使用json.jar将需要发送的数据转换为JSON格式字符串,并设置到请求中的Body中。这样就能够方便地对数据进行处理和验证。 总之,json.jar是JMeter中一个非常实用的扩展包,它提供了对JSON数据的便捷处理功能,可以帮助测试人员更加快速和准确地完成性能测试任务。 ### 回答2: jmeter json.jar包是一个JMeter插件,用于解析和处理JSON格式数据。JSON是一种轻量级的数据交换格式,常见于前后端数据传输及API接口调用中。 jmeter json.jar包可以在JMeter中实现JSON解析和生成,支持多种JSON格式(包括compact、strict、canonical等),可以通过简单的配置和语法实现JSON数据的提取和转换。此外,它还可以将提取的JSON数据作为变量在JMeter中进行断言、参数化、输出等操作。 使用jmeter json.jar包,可以帮助测试人员更方便、更快速地进行JSON数据的处理和验证。在API测试中,通过该插件可以自动化获取和解析API返回的JSON数据,进一步增强测试覆盖度和可靠性。同时,它还可以与其他JMeter插件(如HTTP Sampler、Beanshell等)进行集成,实现一系列复杂的测试场景。 总之,jmeter json.jar包是一个非常有用的JMeter插件,可以帮助测试人员更好地应对JSON数据处理相关的测试任务,提升测试效率和质量。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值