springMVC绑定json参数之二(2.2.2)

二、springmvc 接收不同格式的json字符串

2).格式二:json字符串数组

前台:

test = function () {
        
        
                var  test = ["123","qwe","asd"];

                jQuery.ajax({  
                    url : cur_url+"/weekly/test",  
                    type : 'post',  
                    data : JSON.stringify(test),  
                    dataType : 'json',
                    contentType:'application/json;charset=utf-8',
                    success : function (data, textStatus) { 
                        console.info(data);
                        alert("test success!");
                    },
                    error:function(){
                        alert("test error!");
                    }
                });
    };

后台接收:

        @RequestMapping("/test")
        @ResponseBody
         public List<String> test(@RequestBody String[] strs ) {
           for (String s : strs) {
            System.out.println(s);
           }
        
           return Arrays.asList(strs);
            
      }

3).格式三:json 数字组成的数组
前台发送:

test = function () {
        
        
                var  test = [1231,4562,7893];

                jQuery.ajax({  
                    url : cur_url+"/weekly/test",  
                    type : 'post',  
                    data : JSON.stringify(test),  
                    dataType : 'json',
                    contentType:'application/json;charset=utf-8',
                    success : function (data, textStatus) { 
                        console.info(data);
                        alert("test success!");
                    },
                    error:function(){
                        alert("test error!");
                    }
                });
    };

后台接收:

@RequestMapping("/test")
        @ResponseBody
         public List<Integer> test(@RequestBody Integer[] strs ) {
           for (Integer s : strs) {
            System.out.println(s);
           }
        
           return Arrays.asList(strs);
            
      }

 

posted @ 2017-04-26 12:59  迷走神经  阅读(206)  评论(0编辑  收藏  举报