SpringMVC之通过ajax处理json数组

一、导jar包:

 二、controller.java:

@ResponseBody
	@RequestMapping(value="testJson")
	public List<Student> testJson(){
		Student stu1 = new Student(1,"zs",21);
		Student stu2 = new Student(1,"zs",21);
		List<Student> students = new ArrayList<>();
		students.add(stu1);
		students.add(stu2);
		return students;
	}

  三、jsp:

<script type="text/javascript" src="js/jquery-1.5.1.js"></script>
<title>Insert title here</title>
</head>
<body>
	<script type="text/javascript">
		$(document).ready(function(){
			$("#testJson").click(function(){
				$.post(
						//服务器地址
						"SpringMVCController/testJson",
						//{"name":"zs"},
						function(result){
							for(var i=0;i<result.length;i++){
								alert(result[i].id+"-"+result[i].name+"-"+result[i].age);
							}
						}
						);
			})
		});
	
	</script>
	<input type="button" value="testJson" id="testJson">
		

  

原文地址:https://www.cnblogs.com/jccjcc/p/14050727.html