Jmeter Beanshell 中JSONObject 和JSONArray 处理


import org.json.JSONArray;
import org.json.JSONObject;
import java.util.Calendar;
import java.util.Date;
import java.text.SimpleDateFormat;
// 获取当前日期
Calendar calendar = Calendar.getInstance();
// 设置为当月最后一天
calendar.add(Calendar.MONTH, 1);
calendar.add(Calendar.DATE, -1);
// 将小时、分钟、秒钟、毫秒设为最大值
Date lastDayOfMonth = calendar.getTime();
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
String lastDayStr = dateFormat.format(lastDayOfMonth);

String response = prev.getResponseDataAsString(); // 得到接口响应结果response
JSONObject jsonObject = new JSONObject(response);// 定义json对象
log.info("response:"+jsonObject.toString());
JSONArray exercise_data = jsonObject.getJSONArray("data");
log.info("exercise_data:"+exercise_data.toString());
JSONObject process_save =new JSONObject();
for (int i = 0; i < exercise_data.length(); i++) {
JSONObject data_statistics = exercise_data.getJSONObject(i);
JSONArray statisticsList = data_statistics.getJSONArray("statisticsList");
process_save.put("manufacturingOrderId", data_statistics.get("manufacturingOrderId"));
process_save.put("distributionId", data_statistics.get("distributionId"));
process_save.put("contractNum", data_statistics.get("contractNum"));
process_save.put("type", "1");
process_save.put("deliveryType", "1");
JSONArray innerDispatcherList = new JSONArray();
for (int k = 0; k < statisticsList.length(); k++) {
JSONObject color_info = statisticsList.getJSONObject(k);
JSONObject detail =new JSONObject();
detail.put("orderDetailId", color_info.get("orderDetailId"));
detail.put("srcOrderDetailId", color_info.get("srcOrderDetailId"));
detail.put("orderBatch", color_info.get("orderBatch"));
detail.put("color", color_info.get("color"));
detail.put("skcId", color_info.get("skcId"));
detail.put("operatingDate", lastDayStr);
detail.put("customerDeliveryAddress", color_info.get("deliveryAddress"));
detail.put("customerContactName", color_info.get("contactName"));
detail.put("customerContactPhone", color_info.get("contactPhone"));
JSONArray old_sizeList = color_info.getJSONArray("sizeList");
JSONArray sizeList = new JSONArray();
for (int j = 0; j < old_sizeList.length(); j++) {
JSONObject old_size = old_sizeList.getJSONObject(j);
JSONObject size_info =new JSONObject();
size_info.put("sizeId", old_size.get("sizeId"));
size_info.put("sizeSort", old_size.get("sizeSort"));
size_info.put("sizeName", old_size.get("sizeName"));
size_info.put("sizeExpectCount", "2");
size_info.put("sizeQualityCount", "");
size_info.put("sizeDefectiveCount", "");
size_info.put("pendingCount", "2");
size_info.put("pendingDefectiveCount", old_size.get("pendingDefectiveCount"));
size_info.put("sizeValue", "1");
size_info.put("valueType", "1");
sizeList.put(size_info);
}
detail.put("sizeList",sizeList);
innerDispatcherList.put(detail);
}
process_save.put("innerDispatcherList",innerDispatcherList);
break;
}
vars.putObject("process_save",process_save.toString());

 

posted @ 2023-08-21 18:53  keena_jiao  阅读(289)  评论(0编辑  收藏  举报