前端:

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <title>submitUserList_3</title>
    <meta http-equiv="content-type" content="text/html; charset=utf-8">
    <script th:src="@{/resources/js/js/jquery-3.3.1.min.js}"></script>
    <script type="text/javascript" language="JavaScript">
        function submitUserList_3() {alert("ok");
            var customerArray = new Array();
            //customerArray.push({id: "1", name: "李四", pwd: "123"});
            //customerArray.push({id: "2", name: "张三", pwd: "332"});
            //customerArray.push({title:"标题一",type:2,options:[{option:"选项一"},{option:"选项二"}]});

            customerArray.push({type:"",title:"",url:"",content:"",endTime:"",questionModels:[{title:"标题一",type:2,options:[{option:"选项一"},{option:"选项二"}]}]});


            //customerArray.push({title:"标题二",shareId:1,type:2,option:"选项一,选项二"});
            //customerArray.push({title:"标题二",type:2,options:[{option:"1"},{option:"2"}]});
            $.ajax({
                url: "./questionShare",
                type: "POST",
                contentType : 'application/json;charset=utf-8', //设置请求头信息
                dataType:"json",
                data: JSON.stringify(customerArray),    //将Json对象序列化成Json字符串,JSON.stringify()原生态方法
                //data: $.toJSON(customerArray),            //将Json对象序列化成Json字符串,toJSON()需要引用jquery.json.min.js
                success: function(data){
                    alert(data);
                },
                error: function(res){
                    alert(res.responseText);
                }
            });
        }
    </script>
</head>

<body>
<h1>submitUserList_3</h1>
<input id="submit" type="button" value="Submit" onclick="submitUserList_3();">
</body>

</html>

注:上面嵌套了多个对象:

({type:"",title:"",url:"",content:"",endTime:"",questionModels:[{title:"标题一",type:2,options:[{option:"选项一"},{option:"选项二"}]}]})
最外层对象
public class Share {

    private Integer id;
    private Integer type;
    private String title;
    private String url;
    private String content;
    private String time;
    private String endTime;

    private List<QuestionModel> questionModels;
}
---
questionModels对象:
public class QuestionModel{

    private Integer id;  //标识符
    private String title;  //标题
    private Integer type;    //分享类型
    private List<Option> options;   //选项
}
----
Options对象
public class Option {

    private String option;
}
---
后台接收对象
@RequestMapping("/questionShare")
@ResponseBody
public String getTest3(@RequestBody List<Share> shares){
	System.out.println("记录数:"+shares.size());
	return "success";
}


[{"type":"1","title":"母亲节","url":"","content":"","endTime":"","questionModels":[{"title":"标题一","type":2,"options":[{"option":"选项一"},{"option":"选项二"}]}]}]。
--------------
如果用字符串接收,这是数组类型,需要将"[]"去掉在转换为Java对象
@RequestMapping("updateShare")
    @ResponseBody
    public AjaxResult update(@RequestBody String string){
        System.out.println("接收的参数:"+ string );
        JSONObject jsonObject = JSONObject.parseObject(string);
        Share share = JSON.toJavaObject(jsonObject,Share.class );
        int result = shareService.update(share);
        if(result > 0){
            return AjaxResult.ok();
        }else{
            return AjaxResult.build("false",null );
        }
    }

  dd 

posted on 2019-06-21 10:31  lazyli  阅读(28303)  评论(0编辑  收藏  举报