亲身经历:变量作用域一定要注意,容易把自己绕晕,变量尽可能的要贴近引用的地方
比如
List<Object> eduLearningList = new ArrayList<>();
JSONArray eduLeaningArry = new JSONArray();
DeclareMessage declareMessage = new DeclareMessage();
declareMessage.setDeclareApplyId(apply.getId());
declareMessage.setType(2);//查学习经历
//查询申报填写材料
List<DeclareMessage> declareMessageList = declareMessageService.findByObj(declareMessage);
DeclareMessage message = declareMessageList.get(0);
String content = message.getContent();
//学习经历
JSONObject base = JSON.parseObject(content);
eduLeaningArry = base.getJSONArray("eduLearnings");
System.out.println(eduLeaningArry.size()+"---------------------");
declareMessage.setType(4);//查工作经历
//查询申报填写材料
declareMessageList = declareMessageService.findByObj(declareMessage);
message = declareMessageList.get(0);
content = message.getContent();
//工作经历
JSONObject workUndergoJson = JSON.parseObject(content);
JSONArray workUndergos = workUndergoJson.getJSONArray("workUndergos");
int count = workUndergos.size();
Boolean eduLeaningIsBig = false;
if (eduLeaningArry.size()>workUndergos.size()){
count = eduLeaningArry.size();
eduLeaningIsBig = true;
}
for (int x=0;x<count;x++){
Map<String, String> eduLearningMap = new HashMap<>();
//如果学习经历多的话,工作经历不足的地方置空
if (eduLeaningIsBig) {
if (x < workUndergos.size()) {
//添加工作经历数据
Object workUnder = workUndergos.get(x);
WorkUndergo workUndergo = JSON.parseObject(workUnder.toString(), WorkUndergo.class);
String startDate = workUndergo.getStartDate();
String endDate = workUndergo.getEndDate();
String startDateAndEndDate = startDate + "/" + endDate;//起止时间
String workUnit = workUndergo.getWorkUnit();//工作单位
String departmentW = workUndergo.getDepartment();// 部门
String dutiesW = workUndergo.getDuties();//职务
eduLearningMap.put("workTime", startDateAndEndDate);
eduLearningMap.put("company", workUnit);
eduLearningMap.put("department", departmentW);
eduLearningMap.put("duties", dutiesW);
//添加学习经历数据
Object eduLearningObject = eduLeaningArry.get(x);
EduLearning eduLearnings = JSON.parseObject(eduLearningObject.toString(), EduLearning.class);
String beginDateEdu = eduLearnings.getBeginDate();//起时间
String endDateEdu = eduLearnings.getEndDate();//止时间
String beginAndEndDate = beginDateEdu + "/" + endDateEdu;//起止时间
String cPschool = eduLearnings.getcPschool();//毕业学校
String cPselectMajor = eduLearnings.getcPselectMajor().getLables();//所学专业
eduLearningMap.put("studyTime", beginAndEndDate);
eduLearningMap.put("cPschool", cPschool);
eduLearningMap.put("cPselectMajor", cPselectMajor);
eduLearningList.add(eduLearningMap);
} else {
//工作经历已经存储完 继续存储剩余的学习经历
//添加学习经历数据
Object eduLearningObject = eduLeaningArry.get(x);
EduLearning eduLearnings = JSON.parseObject(eduLearningObject.toString(), EduLearning.class);
String beginDateEdu = eduLearnings.getBeginDate();//起时间
String endDateEdu = eduLearnings.getEndDate();//止时间
String beginAndEndDate = beginDateEdu + "/" + endDateEdu;//起止时间
String cPschool = eduLearnings.getcPschool();//毕业学校
String cPselectMajor = eduLearnings.getcPselectMajor().getLables();//所学专业
eduLearningMap.put("studyTime", beginAndEndDate);
eduLearningMap.put("cPschool", cPschool);
eduLearningMap.put("cPselectMajor", cPselectMajor);
eduLearningList.add(eduLearningMap);
}
}else {
//如果工作经历多的话,学习经历不足的地方置空
if(x<eduLeaningArry.size()){
//添加学习经历数据
Object eduLearningObject = eduLeaningArry.get(x);
EduLearning eduLearnings = JSON.parseObject(eduLearningObject.toString(), EduLearning.class);
String beginDateEdu = eduLearnings.getBeginDate();//起时间
String endDateEdu = eduLearnings.getEndDate();//止时间
String beginAndEndDate = beginDateEdu + "/" + endDateEdu;//起止时间
String cPschool = eduLearnings.getcPschool();//毕业学校
String cPselectMajor = eduLearnings.getcPselectMajor().getLables();//所学专业
eduLearningMap.put("studyTime", beginAndEndDate);
eduLearningMap.put("cPschool", cPschool);
eduLearningMap.put("cPselectMajor", cPselectMajor);
//添加工作经历数据
Object workUnder = workUndergos.get(x);
WorkUndergo workUndergo = JSON.parseObject(workUnder.toString(), WorkUndergo.class);
String startDate = workUndergo.getStartDate();
String endDate = workUndergo.getEndDate();
String startDateAndEndDate = startDate + "/" + endDate;//起止时间
String workUnit = workUndergo.getWorkUnit();//工作单位
String departmentW = workUndergo.getDepartment();// 部门
String dutiesW = workUndergo.getDuties();//职务
eduLearningMap.put("workTime", startDateAndEndDate);
eduLearningMap.put("company", workUnit);
eduLearningMap.put("department", departmentW);
eduLearningMap.put("duties", dutiesW);
eduLearningList.add(eduLearningMap);
}else {
//学习经历已经存储完 继续存储剩余的工作经历
//添加工作经历数据
Object workUnder = workUndergos.get(x);
WorkUndergo workUndergo = JSON.parseObject(workUnder.toString(), WorkUndergo.class);
String startDate = workUndergo.getStartDate();
String endDate = workUndergo.getEndDate();
String startDateAndEndDate = startDate + "/" + endDate;//起止时间
String workUnit = workUndergo.getWorkUnit();//工作单位
String departmentW = workUndergo.getDepartment();// 部门
String dutiesW = workUndergo.getDuties();//职务
eduLearningMap.put("workTime", startDateAndEndDate);
eduLearningMap.put("company", workUnit);
eduLearningMap.put("department", departmentW);
eduLearningMap.put("duties", dutiesW);
eduLearningList.add(eduLearningMap);
}
}
}
data.put("undergo",eduLearningList);
这块代码中eduLearningMap跟eduLearningList的相对位置要注意;如果把eduLearningMap拎出来放在eduLearningList同级的位置,程序也不会报错,只是组装的数据都是eduLearningMap的最后一个数据;重复的数据;eduLearningMap的引用数据为题