ajax向后台java程序里传递对象数组

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u014595589/article/details/68925291

前台:

function saveList(group_id){        
           if(!confirm("确认提交?"))
                 return;
           var Html=$('#List').children('li');
           var List=[];
           for(var i=0;i<notAuditInsHtml.length;i++)
           {
                var E=new Object();
                E.a=notAuditInsHtml.eq(i).children('.a').html();
                E.b=notAuditInsHtml.eq(i).children('.b').html();
                List.push(E);
           }
             var url = "task/saveList";
    	     $.ajax({
    		    type: "post",
    		    async:  false,
    		    url: url,
    		    data: {"id":id,"list":JSON.stringify(List)},
    		    dataType: "text",
    		    beforeSend: function() {
    			
               },
    		    success: function(data){
    			   if(data=="success")
    			      alert("提交成功");
    			    else alert("提交失败,请稍后再试");
    			    return;		
    		  }});
        }


后台:

	/**
	 * 保存分组情况
	 * @param response
	 * @param model
	 */
	@ResponseBody
	@RequestMapping(value="/saveList",method=RequestMethod.POST)
	public String saveGroupByGroupId(int id,String list,response,Model model){
                         JSONObject  m=JSONArray.fromObject(list).getJSONObject(0);	
		       System.out.println(m.get("a"));
                         return "success";
    }

可以定义一个类E,包含属性a和b,并且有set和get方法。

函数参数可以变成List<E> list;

或者对m进行转换,即E e = (E)JSONObject.toBean(m, E.class);   


 

猜你喜欢

转载自blog.csdn.net/u014595589/article/details/68925291