@RequestMapping(value = {"save"}) @ResponseBody public Result save(TBaseInterventionPlan model,String tBaseRegulateInterventions) { Result result; List<TBaseRegulateIntervention> list = new ArrayList<TBaseRegulateIntervention>(); if (StringUtils.isNotBlank(tBaseRegulateInterventions)) { JSONArray json = JSONArray.parseArray(tBaseRegulateInterventions); // 首先把字符串轉成 JSONArray 對象 if (json.size() > 0) { for (int i = 0; i < json.size(); i++) { JSONObject job = json.getJSONObject(i); // 遍歷 jsonarray 數組,把每一個對象轉成 json 對象 TBaseRegulateIntervention t = new TBaseRegulateIntervention(); t.setRegulateId(job.get("regulateId").toString()); t.setContent(job.get("content").toString()); list.add(t); } } } //保存更改干預方案 iTBaseRegulateInterventionService.update(model, list); result = Result.success(); return result; }
public void update(TBaseInterventionPlan model, List<TBaseRegulateIntervention> tBaseRegulateInterventions){ List<TBaseRegulateIntervention> list = new ArrayList<TBaseRegulateIntervention>(); Map<String,String> map = new HashMap<String,String>(); if(StringUtils.isNotBlank(model.getId())){ TBaseRegulateIntervention tBaseRegulateIntervention = new TBaseRegulateIntervention(); tBaseRegulateIntervention.setInterventionId(model.getId()); //通過干預方案id查找關聯表中的方案內容 list = tBaseRegulateInterventionMapper.select(tBaseRegulateIntervention); if (CollectionUtils.isNotEmpty(list) && CollectionUtils.isNotEmpty(tBaseRegulateInterventions)){ //將查出來的list保存到map for(TBaseRegulateIntervention t : tBaseRegulateInterventions){ map.put(t.getRegulateId(),t.getContent()); } //將前台傳進來的list和數據庫的list比較,改變了就更新 for(TBaseRegulateIntervention s : list){ System.out.print("++++++++++++++" + s.getRegulateId()); System.out.print("***************" + s.getContent()); if(!s.getContent().equals(map.get(s.getRegulateId()))){ System.out.print("-----------------" + map.get(s.getRegulateId())); s.setContent(map.get(s.getRegulateId())); tBaseRegulateInterventionMapper.updateByPrimaryKeySelective(s); } } } //干預方案id不為空就更改干預方案 iTBaseInterventionPlanService.updateByPKNotNull(model); }else { model.setCreateTime(new Date()); //干預方案id為空就保存干預方案 iTBaseInterventionPlanService.save(model); if(CollectionUtils.isNotEmpty(tBaseRegulateInterventions)){ for (TBaseRegulateIntervention r : tBaseRegulateInterventions) { TBaseRegulateIntervention tBaseRegulateIntervention = new TBaseRegulateIntervention(); tBaseRegulateIntervention.setRegulateId(r.getRegulateId()); tBaseRegulateIntervention.setInterventionId(model.getId()); tBaseRegulateIntervention.setContent(r.getContent()); tBaseRegulateIntervention.setCreateTime(new Date()); this.save(tBaseRegulateIntervention); } } } }
很久沒寫博客了,除了工作忙懶得寫之外,還有第一篇關於FQ的博客被系統刪了讓我很無語,這次就記錄一下JSON解析的問題,以后再遇到可以快速找到類似的解決方法。