前端传输map集合数据到后台

js:

function createUrl() {
    var map = {};
    $("#channelTable").bootstrapTable('getSelections').forEach(function (item) {
        map[item.id]=item.number;
    });
    if (map.size <= 0) {
        toastr.error("请至少选择一行记录");
        return;
    }

    $.ajax({
        url: 'channel/createChannelUrl',
        method: 'post',
        contentType: 'application/json;charset=utf-8',
        data: JSON.stringify(map),
        success: function () {
            toastr.success("添加渠道url成功");
            $("#channelTable").bootstrapTable('refresh');
        },
        error: function (err) {
            if (400 === err.status) {
                toastr.error(err.responseText);
            } else {
                toastr.error('添加渠道信息失败')
            }
        }
    });
}

controller:

   @PostMapping("createChannelUrl")
    public ResponseEntity<Void> createChannelUrl(@RequestBody Map<String, String> map) {
      channelService.createChannelUrl(map);
        return ResponseEntity.ok().build();
    }

service:

 @Override
    public 	int createChannelUrl( Map<String, String> map){
        //遍历map
        for(Map.Entry<String, String> entry : map.entrySet()){
            String mapKey = entry.getKey();
            String mapValue = entry.getValue();
            JsonArray jsonArray = HttpRequestUtil.getXpath(mapValue);
            
            List<ChannelUrl> channelUrlList = new ArrayList<ChannelUrl>();
            log.info(jsonArray.toString()+jsonArray.size());

            for (int i = 0; i < jsonArray.size(); i++) {
                log.info(jsonArray.get(i).toString());
                JsonObject jsonObject = (JsonObject) jsonArray.get(i);
                channelUrlList.add(new ChannelUrl()
                        .setCategory("0")
                        .setDownCount("0")
                        .setUrl(jsonObject.get("url").toString().replaceAll("\"",""))
                        .setChannelId(Integer.parseInt(mapKey))
                        .setJson(jsonObject.toString())
                        .setKwd(jsonObject.get("kwd").toString().replaceAll("\"",""))
                );

            };
            return channelUrlMapper.insert(channelUrlList);
        }

        return 1;
    }
  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值