比如说我们要取以下json里的evaluateTaskId,怎么办呢?如果这个evaluateTaskId,有多个,不至一个,怎么办?
1,首先要分析这个json的构成
2,再根据这个组成,一步一步去取这个id
3,先写好这个接口的http请求,
4,在这个请求上加一个后置处理器
5,再写上beanshell就ok
{
"success": true,
"content": {
"total": 85,
"size": 10,
"records": [
{
"evaluateTime": 1567664252000,
"settleStatus": 0,
"planName": "言言言言言言言言言言",
"evaluateTaskId": "1567664251852902",
"realStartTime": 1567664160000,
"nowTime": 1567673102000,
"complaintStatus": 9999,
"evaluateStatus": 7,
"buyAccount": "yb18737280039",
"evaluateFlag": true,
"planId": "1567664094840558",
"id": "1567664094911615",
"keyword": "恭恭敬敬恭恭敬敬恭恭敬敬",
"markDescribe": null,
"realTimeFlag": false,
"realEndTime": 1567678560000,
"orderNo": null,
"settleTime": 1567664181000,
"auditMode": 2,
"addToEvaluateStatus": 9999,
"webStoreName": "",
"totalAmount": 321.36,
"shokeyStatus": 3,
"executeTaskId": "1567664094912616",
"promoterStatus": 3,
"evaluateComplaintStatus": 9999,
"orderPayTime": null,
"mark": null,
"status": 4
},
{
"evaluateTime": 1567666904000,
"settleStatus": 0,
"planName": "言言言言言言言言言言",
"evaluateTaskId": "1567666904026080",
"realStartTime": 1567664174000,
"nowTime": 1567673102000,
"complaintStatus": 9999,
"evaluateStatus": 7,
"buyAccount": "yb18737280039",
"evaluateFlag": true,
"planId": "1567664094840558",
"id": "1567664094904609",
"keyword": "禾禾禾禾禾禾",
"markDescribe": null,
"realTimeFlag": false,
"realEndTime": 1567678574000,
"orderNo": null,
"settleTime": 1567664181000,
"auditMode": 2,
"addToEvaluateStatus": 9999,
"webStoreName": "AAAAAAAA",
"totalAmount": 321.36,
"shokeyStatus": 3,
"executeTaskId": "1567664094904610",
"promoterStatus": 3,
"evaluateComplaintStatus": 9999,
"orderPayTime": null,
"mark": null,
"status": 4
},
jmeter截图如下:
beanshell代码如下:
/*JMETER,BEANSHELL循环遍历值,随机取数组当中一个值*/
import org.json.*;
import org.apache.jmeter.samplers.SampleResult;
import java.util.ArrayList;
import java.util.List;
import java.sql.ResultSet;
import java.io.*;
import java.text.DecimalFormat;
String response_data = prev.getResponseDataAsString();
String request_data = prev.getQueryString();
String tname = prev.getThreadName();
log.info("==========="+ response_data);
JSONObject data_obj = new JSONObject(response_data);
//得到数组records,并转成字符串
String apps_str = data_obj.get("result").get("list").toString();
log.info("=======数组的值为: "+ apps_str);
JSONArray apps_array = new JSONArray(apps_str);
for(int i=0;i
String interfaceId = app_obj.get("interfaceId").toString();
log.info("=============循环打印interfaceId的值为" + interfaceId );
}
/*随机得到数组内的一个值,并把值传到下个接口*/
int ranLen = apps_array.length();
Random r=new Random();
int i1=r.nextInt(ranLen); //生成[0, apps_array.length()]区间的整数
log.info(">>>>>>>" + i1+"" );
JSONObject app_obj = new JSONObject(apps_array.get(i1).toString());
//从app_obj这个对像中获得coinChange,转化成字符串
String interfaceId = app_obj.get("interfaceId").toString();
vars.put("interfaceId",interfaceId);
log.info("随机得到的值为:" + interfaceId );