日期:2014-05-16  浏览次数:20401 次

struts2里面使用JSON
页面引进jquery
<script  type="text/javascript" src="<%=request.getContextPath()%>/js/jquery.js"></script>
function test(){
	var url = "/donategroup/queryMemberDonate.action";
	$.getJSON(url, function(json){
		alert(json);//json是一个json数组
		 for(var i=0;i<json.length;i++){
		 	alert(json[i]);
		 	alert('amount:'+json[i].amount);
		 	alert('bankName:'+json[i].bankName);
		 	alert('memberId:'+json[i].memberId);
		 }
	});
}

在queryMemberDonate.action里面查询出来一个list,
转换成json,ps:MemberDonateRecord对象有的属性:amount、bankName、memberId等等
List<MemberDonateRecord> memberdonaterecord=donaterecordservice.queryMemberDonate(memberId,requestId,page,size,name,beginDate,endDate);
JSONArray ja = JSONArray.fromObject(memberdonaterecord);
ServletActionContext.getResponse().setCharacterEncoding("UTF-8");
final PrintWriter out = ServletActionContext.getResponse().getWriter();
out.write(ja.toString());