struts2的list对象json简单输出(希望对大家有帮助)

struts2的list对象json输出,希望对大家有帮助,有问题的请留言

	/**
		 * 将list对象封装成jsonArray
		 */
		JSONArray jsonArray=JSONArray.fromObject(slist);
		
		HttpServletResponse response = (HttpServletResponse) ActionContext.getContext().get(ServletActionContext.HTTP_RESPONSE);   
		/**
		 * 输出到客户端相关的cache,字符集设置
		 */
		response.setContentType("text/html;charset=utf-8");
		response.setCharacterEncoding("utf-8");
		response.setHeader("Charset", "utf-8");
		response.setHeader("Cache-Control", "no-cache");

		PrintWriter out = response.getWriter();
		/**
		 * 输出到客户端json对象
		 */
		out.write(jsonArray.toString());
		
		out.flush();
		out.close();


 
function GetStudent()
   {   
	   $.ajax({   
	        type:'post',   
	        url:"/stu/listStu/listStudent",   
	        dataType: 'json',   
	        success: function(data){   
	        	//alert(data);
	        	for(var i in data)
	        	alert("name-->"+data[i].name);
	        },   
	        error: function(XMLHttpRequest, textStatus, errorThrown){
	        	alert(XMLHttpRequest);
	        	alert(textStatus);
	        	alert(errorThrown);
	            return;   
	        }   
	    });     
   }
   </script>


所需的jar包
struts2的list对象json简单输出(希望对大家有帮助)
下面有该Demo的源代码

你可能感兴趣的:(json,struts)