jmeter-后置处理器-BeanShell PostProcessor-json提取-json值修改-get

1、导入json包

2、import JSON jar包-时间格式的包(修改值用到了)

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
//import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

3、获取系统变量-responsedata 

String response_data = prev.getResponseDataAsString();

4、修改json的某个字段  data.param.ciType.description 修改这个字段

JSONObject jsonObject=JSON.parseObject(response_data);
JSONObject paramJson = (JSONObject)((JSONObject) jsonObject.get("data")).get("param");
JSONObject ciTypeJsom = (JSONObject)paramJson.get("ciType");
ciTypeJsom.put("description",ciTypeJsom.get("description")+"_auto_"+LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmm")));

//SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMddHHmm");
//ciTypeJsom.put("description",ciTypeJsom.get("description")+"_auto_"+sdf.format(new Date()));
String responseData_a=jsonObject.toString();

5、系统时间-两种获取方法,记得导入包--原来description变为description值加+_auto_+时间

import java.text.SimpleDateFormat;

SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMddHHmm");
ciTypeJsom.put("description",ciTypeJsom.get("description")+"_auto_"+sdf.format(new Date()));
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

ciTypeJsom.put("description",ciTypeJsom.get("description")+"_auto_"+LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmm")));

6、保存变量

vars.put("responseData_b", responseData_a);

7、获取json中的字符串-下面的转的


原文链接:https://blog.csdn.net/aduocd/java/article/details/80351719

1. 场景一:获取请求响应中的数据,并保存

import com.alibaba.fastjson.*;  // 引入包。这个包需要先放在:<安装目录>\apache-jmeter-3.2\lib\ext中

// 获取数据

String response = prev.getResponseDataAsString();  // 获取Response

JSONObject responseObj = JSON.parseObject(response);  // 整个Response作为JSON对象

JSONObject resObj = responseObj.getJSONObject("res");  // 获取某一部分对象。即,json串中{}的内容

JSONArray listArray = resObj.getJSONArray("list");  // 获取列表。即,json串中[]的内容

// 保存数据

// 1) 列表

int len = listArray.size();

for(int i=0; i<len; i++){

    temp[i] = listArray.get(i).getString("name");

}

vars.put("names", Arrays.toString(temp));  // 保存到JMeter变量中,vars.put只能保存字符串,因此这里使用了toString()进行转换

// 2) String类型

String id = resObj.get("id");  // 获取对象中某一字符串的内容。

vars.put("id", id);  // 保存到JMeter变量中,后面的组件可以使用

// 3) Integer类型

Integer no = resObj.get("id"); // 获取对象中某一整型的内容。

vars.put("no", no.toString());   // 保存到JMeter变量中,vars.put只能保存字符串,因此这里使用了toString()进行转换

2. 场景二:获取数据库中查询到的数据,并保存

前提:在JDBC Request中已经获取到数据,详见《JDBCRequest的用法》。

select total_cash from t_cash where user_name="tom";

在Result variable name中的值为:result

String cash_new = vars.getObject("result").get(0).get("total_cash").toString();

vars.put("cash_new", cash_new);  // 操作前与此步骤相同,不再赘述。保存的变量名为cash_old

猜你喜欢

转载自www.cnblogs.com/shishibuwan/p/12683455.html