ajax提交JSON数组及Springboot接收转换为list类

版权声明:本文为博主原创文章,欢迎转载。 https://blog.csdn.net/chengyuqiang/article/details/91379102

1、AJAX传递JSON数据

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<script type="text/javascript" src="/bloomberg/js/jquery-3.3.1.js"></script>
<script type="text/javascript" th:inline="javascript">
    function save() {
        var data = JSON.stringify({d_date: "2019-06-06",vc_exchange : "IB",vc_symbol:"038007",
                                    vc_symbol_id:"038007.IB",vc_name:"03中铁债",vc_type:"KRD",
                                    vc_type_detail:"KRD_3MO",f_value:"0.009399",vc_source:"test",d_updatetime:"2019-06-06 11:11:28"})
        $.ajax({
            type : "post",
            url : "/bloomberg/save",
            data : data,
            contentType: 'application/json',
            success : function (result) {
                console.log(result);
            }
        })
    };
    save();
</script>

<body>

</body>
</html>
    @RequestMapping(value="/save",method = RequestMethod.POST,consumes = "application/json")
    @ResponseBody
    public String save(@RequestBody XxxBean bean){
        System.out.println(bean);
        return "success";
    }

2、AJAX传递JSON数组

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<script type="text/javascript" src="/bloomberg/js/jquery-3.3.1.js"></script>
<script type="text/javascript" th:inline="javascript">
    function saveAll() {
        var data = [
            {
                 d_date: "2019-06-06",vc_exchange : "IB",vc_symbol:"038007",
                 vc_symbol_id:"038007.IB",vc_name:"03中铁债",vc_type:"KRD",
                 vc_type_detail:"KRD_3MO",f_value:"0.009399",vc_source:"Bloomberg",d_updatetime:"2019-06-06 11:11:28"
            }, {
                d_date: "2019-06-05",vc_exchange : "IB",vc_symbol:"058028",
                vc_symbol_id:"058028.IB",vc_name:"05中远债2",vc_type:"KRD",
                vc_type_detail:"KRD_9YR",f_value:"0.000000",vc_source:"test",d_updatetime:"2019-06-05 14:08:40"
            }];
        $.ajax({
            type : "post",
            url : "/bloomberg/saveAll",
            data : JSON.stringify({beans:data}),
            contentType: 'application/json',
            success : function (result) {
                console.log(result);
            }
        })
    };
    saveAll();
</script>

<body>

</body>
</html>

不能直接使用JSONArray类型的参数接收前台的json数组字符串。曲线救国:用JSONObject接收,然后取出JSONArray。

    @RequestMapping(value="/saveAll",method = RequestMethod.POST,consumes = "application/json")
    @ResponseBody
    public String saveAll(@RequestBody JSONObject jsonObject){
        JSONArray jsonArray = jsonObject.getJSONArray("beans");
        List<XxxBean> list=(List)JSONArray.toCollection(jsonArray,XxxBean.class);
        xxxService.saveAll(list);
        return "successAll";
    }

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

扫码关注云+社区

领取腾讯云代金券