后台运行结果: 前台运行结果:
first.jsp:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> <script type="text/javascript" src="jQuery/jquery-1.9.1.min.js"></script> <script type="text/javascript"> function test2(){ //用这种方式组装的json数组是正确的 var testArray = new Array(); for(var i = 0; i < 5; i++) { var tempArray = new Array(); for(var j = 0; j < 10; j++){ tempArray.push(i*j); } testArray.push(tempArray); } alert("tempArray=" + tempArray); //用这种方式组装的json数组是正确的 var jsonArr = new Array(); for ( var j = 0; j < 3; j++) { var jsonObj = {}; jsonObj["gradeId"]=1; jsonObj["gradeName"]=2; jsonObj["level"]=3; jsonObj["boundary"]=4; jsonObj["status"]=5; jsonArr.push(jsonObj) } alert("jsonArr=" + jsonArr); // 用这种方式组装的json数组是错误的,接收侧只能接收到整个的这个字符串 var employees='['; for ( var j = 0; j < 5; j++) { employees+= '{'; employees+="name:"; employees+="zhang"; employees+=","; employees+="old:"; employees+=30 + j; employees +='}'; if(j!=4){ employees+=',' }; } employees+=']'; alert("employees=" + employees); $.ajax({ type:"POST", //请求方式 url:"./testJson", //请求路径 cache: false, data:{//传参 "name":"zhang3", "testArray":testArray, "students": //用这种方式传递多维数组也是正确的 [ {"name":"jackson","age":100}, {"name":"michael","age":51} ], "employees":employees, "jsonArr":jsonArr }, dataType: 'json', //返回值类型 success:function(json){ alert(json[0].username+" " + json[0].password); //弹出返回过来的List对象 alert(json[1].username+" " + json[1].password); //弹出返回过来的List对象 } }); } </script> </head> <body> <input type="button" name="b" value="TestJson" onclick="test2()"/> </body> </html>
TestJson.java
package com.zm.servlet; import java.io.BufferedReader; import java.io.IOException; import java.io.PrintWriter; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import net.sf.json.JSONArray; import net.sf.json.JSONObject; public class TestJson extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request,response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); System.out.println("into doPost"); //用map来接收request发送过来的多维数组 Map map = request.getParameterMap(); Iterator<String> iter = map.keySet().iterator(); while (iter.hasNext()) { String key = iter.next(); System.out.println("key=" + key ); String[] value = (String[]) map.get(key); System.out.print("value="); for(String v:value){ System.out.print(v + " "); } System.out.println(); } String sName= request.getParameter("name");//得到ajax传递过来的paramater System.out.println(sName); PrintWriter printWriter = response.getWriter(); List list = new ArrayList();//传递List Map m=new HashMap();//传递Map User u1=new User(); u1.setUsername("zah"); u1.setPassword("123"); User u2=new User(); u2.setUsername("ztf"); u2.setPassword("456"); list.add(u1); //添加User对象 list.add(u2); //添加User对象 m.put("u1", u1); m.put("u2", u2); JSONArray jsonArray2 = JSONArray.fromObject( list ); //把java数组转化成转化成json对象 //JSONObject jsonObject =JSONObject.fromObject(m);//转化Map对象 printWriter.print(jsonArray2);//返给ajax请求 //printWriter.print(jsonObject);//返给ajax请求 printWriter.flush(); printWriter.close(); System.out.println("finish"); } }
web.xml:
<!-- 测试jquery json--> <servlet> <servlet-name>testJson</servlet-name> <servlet-class>com.zm.servlet.TestJson</servlet-class> </servlet> <servlet-mapping> <servlet-name>testJson</servlet-name> <url-pattern>/testJson</url-pattern> </servlet-mapping>