张晨的个人博客

java转换list为json格式,并在jsp遍历json

张晨的个人博客2014-04-29综合技术 2415 0A+A-

学生类:

public class Student {
	Integer id; //编号
	String name;//姓名
	String gender;//性别
	
	public Student(Integer id, String name, String gender) {
		super();
		this.id = id;
		this.name = name;
		this.gender = gender;
	}
	
	public Student() {
		super();
	}
}

 

后台controller层代码使用的是ajax调用

String str=JSONArray.fromObject(list).toString();		
PrintWriter pw = res.getWriter();
pw.print(str);
pw.flush();
pw.close();

jsp使用ajax访问后台接收json数据并使用$.each方法进行遍历json

 $.ajax({
	 url : "controller地址",
	 type : "post",
	 dataType : "json", 
	 async : false,
	 data : {"complaint_type_code" : complaint_type_code},
	 success : function(data) {
	 //使用$.each遍历json数据
	 $.each(data,function(index,context){
		 //使用context.属性的方式进行获取数据
		  alert("姓名:"+context.name+"--性别:"+context.gender);
		});
	 }
 });
 

 

文章关键词
list转json
json
list
遍历json
发表评论