fastjson下載下傳和org.json下載下傳,在本文最後面附上下載下傳連接配接。自行下載下傳。
jmeter beanshell 擷取浮點、整數型、字元串型變量
代碼如下:
float test= Float.parseFloat(vars.get("test")); //擷取浮點數test變量的值
int test= Integer.parseInt(vars.get("test"));//擷取整數型test變量的值
String test= vars.get("test");//擷取字元串型test變量的值
精確定留小數點後兩位
1、jmeter beanshell 求精确到最後兩位數,要求四舍五入
代碼如下:
import java.text.DecimalFormat;
//四舍五入
DecimalFormat mFormat = new DecimalFormat("#.00");
String test = mFormat.format(test); //将test變量四舍五入
2、jmeter beanshell 求精确到最後兩位數,直接截取不做四舍五入
代碼如下:
import java.text.DecimalFormat;
DecimalFormat mFormat = new DecimalFormat("0.00");
String test = mFormat.format(test); //将test變量直接截取保留小數點後兩位,不做四舍五入
beanshell--替換數組中的某個字段/替換json的某key的value值,直接用jsonobject.put("isShow", true);
舉例如下:
/* 傳回的response data 資訊參考如下
{
"code":200,
"data":{
"auditor":null,
"status":1,
"storageGoodsList":[
{
"abstracts":null,
"allocateQuantity":null,
"allocateReason":null,
"businessType":"DGRK",
"createdTime":"2020-08-06 17:31:08",
"createdUser":null,
"expireTime":"2020-08-06 00:00:00",
"goodsId":"1290829975123123",
"goodsName":"商品測試",
"goodsNo":"10011003100400020",
"goodsStock":0,
"id":"1291305800404307971",
"inSurplusQuantity":null,
"isShow":0,
"meteringUnit":"個(1箱)",
"outQuantity":13,
"outSurplusQuantity":null,
"perPrice":100.00,
"productNo":"H012020062900000144",
"productTime":"2020-08-06 00:00:00",
"profit":39.00,
"promotionId":null,
"purchasePrice":100.00,
"realInQuantity":13,
"relationId":"1291305800404307970",
"remark":"物品資訊備注---自動化測試腳本",
"saleAmount":1300.00,
"sellingPrice":103.00,
"skuValue":"重量:200g;包裝:10小包;",
"stockBatchNo":"200806173100001",
"stockUnit":"個(1箱)",
"storageCode":"DG320506001",
"storageName":"倉庫測試)",
"subtotalAmount":1300.00,
"surplusQuantity":null,
"taxRate":1.00,
"totalAmount":1287.13,
"typeCode":null,
"unitRate":1.00,
"updatePurPrice":null,
"updateStock":null,
"updatedTime":null,
"updatedUser":null
}
],
"supplierId":0,
"supplierName":"供應商A",
"typeCode":"DG_IN_STORAGE_1"
},
"message":"請求成功",
"success":true
}
*/
import java.util.*;
import com.alibaba.fastjson.*;
String response_data = prev.getResponseDataAsString();
JSONObject data_obj = JSONObject.parseObject(response_data);
//傳回response中擷取數組storageGoodsList
JSONArray dataArray = data_obj.get("data").getJSONArray("storageGoodsList");
//數組轉成JSONObject。第一組
JSONObject storageGoodsList=(JSONObject)dataArray.getJSONObject(0);
//log.info("列印====================================="+storageGoodsList);
//替換JSONObject中的isShow為true
storageGoodsList.put("isShow", true);
//log.info("列印====================================="+storageGoodsList);
//JSONObject再換成字元串
String str = JSON.toJSONString(storageGoodsList);
String goodsList = "["+str+"]";
log.info("列印====================================="+goodsList);
vars.put("goodsList",goodsList);
jmeter beanshell 求範圍内随機數
方式1、
//取一定範圍内的随機數,前閉後開
public static int getRandNo(int min,int max){
Random random = new Random();
int randNo = random.nextInt(max)%(max-min+1) + min;
return randNo;
}
方式2、
//取一定範圍内的随機數,前閉後閉
public static int getRandNo(int min,int max){
Random random = new Random();
int randNo = random.nextInt(max-min+1) + min;
return randNo;
}
有兩個json包,大同小異。請自己選擇。一個是阿裡巴巴的json包,一個是普通的json包。
連結:https://pan.baidu.com/s/1qnH1Nf0zFnDRtFb8gw0uag
提取碼:txym
複制這段内容後打開百度網盤手機App,操作更友善哦
