一,用到的jar包
1.后期添加。
1.js代码
function showcomplex(dataResponse)
{
var data = eval(‘(‘ + dataResponse.responseText + ‘)‘);
var str=‘‘;
for(var i=0;i<data.js.length;i++)
{
str+=‘<ul>‘;
str+=‘<li>‘+data.js[i].id+‘</li>‘;
str+=‘<li>‘+data.js[i].age+‘</li>‘;
str+=‘<li>‘+data.js[i].name+‘</li>‘;
str+=‘<li>‘+data.js[i].address+‘</li>‘;
str+=‘</ul>‘;
}
document.getElementById("content").innerHTML=str;
}
//获取对象级复杂数据
function getcomplex(){
var url = ‘test.do‘;
var pars = ‘method=getComplex‘;
var ajax = new Ajax.Request(
url,
{method:‘post‘,parameters:pars,onComplete:showcomplex}
);
}
2.java 代码(后台)
public ActionForward getComplex(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
response.setContentType("text/html; charset=GBK");
try
{
PrintWriter out = response.getWriter();
JSONObject obj = new JSONObject();
JSONArray js = new JSONArray();
//这里的数据拼装一般是从数据库查询来的
for(int i=0;i<3;i++)
{
JSONObject objtemp = new JSONObject();
objtemp.put("id", i);
objtemp.put("age", "23");
objtemp.put("name", "test"+i);
objtemp.put("address", "test");
js.add(objtemp);
}
obj.put("js",js);
out.print(obj.toString());
}catch(Exception e)
{
e.printStackTrace();
System.out.print("消费明细json存储异常");
}
return null;
}
}