1.下载
json-lib-2.4-jdk15.jar,commons-beanutils.jar,commons-httpclient.jar,commons-lang.jar,ezmorph.jar,morph-1.0.1.jar
2.程序中引入所需的类文件:
import net.sf.json.JSONObject;
3.编写Java文件:
public ActionForward test3(ActionMapping mapping,ActionForm form,
HttpServletRequest request,HttpServletResponse response)
throws Exception {
request.setCharacterEncoding("utf-8");
response.setContentType("text/html");
PrintWriter out = response.getWriter();
Map<String,Object> map= new HashMap<String,Object>();
List<Category> list=new ArrayList<Category>();
Category c=new Category();
c.setName("jianping");
c.setFid(2);
Category c2=new Category();
c2.setName("yize");
c2.setFid(3);
list.add(c);
list.add(c2);
map.put("total",123);
map.put("list",list);
out.print(JSONObject.fromObject(map).toString());
out.flush();
out.close();
return null;
}
function sayHello3() {
dojo.xhrGet({
url: "<%=request.getContextPath()%>/test.do?method=test3",//修改成相应的路径
handleAs: "json",0)">此处必为json
load: function(data)
{
var total=data.total;//提取元素total
alert(total);
var items=data.list; //提取元素list for(var i=0;i<items.length;i++) { alert(items[i].name); } },error: function(response) { alert("Error"+response.message); } }); }