java后台解析前端传来的json

   @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解析的问题,以后再遇到可以快速找到类似的解决方法。

转载于:https://www.cnblogs.com/yuanzipeng/p/8182575.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值