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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM