1. Import json package
2. Import JSON jar package-time format package (modified value used)
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject;
//import java.text.SimpleDateFormat; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter;
3. Get the system variable -responsedata
String response_data = prev.getResponseDataAsString();
4. Modify a field of json data.param.ciType.description Modify this field
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. System time-two ways to get it, remember to import the package-the original description becomes the description value plus + _auto_ + time
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. Save variables
vars.put("responseData_b", responseData_a);
7. Get the string in json-the following transfer
Original link: https://blog.csdn.net/aduocd/java/article/details/80351719
1. Scenario 1: Obtain the data in the request response and save it import com.alibaba.fastjson. *; // Import package. This package needs to be placed in: <install directory> \ apache-jmeter-3.2 \ lib \ ext // Get data String response = prev.getResponseDataAsString (); // Get Response JSONObject responseObj = JSON.parseObject (response); // The whole Response as JSON object JSONObject resObj = responseObj.getJSONObject ("res"); // Get a certain Part of the object. That is, the content of {} in the json string JSONArray listArray = resObj.getJSONArray ("list"); // Get the list. That is, the contents of [] in the json string // Save data // 1) List int len = listArray.size(); for(int i=0; i<len; i++){ temp[i] = listArray.get(i).getString("name"); } vars.put ( "names", Arrays.toString (temp)); // Save to JMeter variable, vars.put can only save string, so toString () is used here for conversion // 2) String type String id = resObj.get ("id"); // Get the content of a string in the object. vars.put ( "id", id); // Save to JMeter variable, the following components can be used // 3) Integer type Integer no = resObj.get ("id"); // Get the content of an integer in the object. vars.put ( "no", no.toString ()); // Save to JMeter variable, vars.put can only save string, so toString () is used here for conversion 2. Scenario 2: Obtain the data queried in the database and save it Premise: The data has been obtained in JDBC Request. For details, please refer to "Usage of JDBCRequest". select total_cash from t_cash where user_name="tom"; The value in Result variable name is: result String cash_new = vars.getObject("result").get(0).get("total_cash").toString(); vars.put ( "cash_new", cash_new); // The same as this step before operation, no more details. The saved variable is named cash_old