先建一个User类:
Java代码
package com.json;
public class User {
String username;
String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
再建一个servlet
Java代码
package com.json;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.json.JSONArray;
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");
String str= request.getParameter("name");//得到ajax传递过来的paramater
System.out.print(str);
PrintWriter out = 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 jo=JSONObject.fromObject(m);//转化Map对象
out.print(jsonArray2);//返给ajax请求
out.print(jo);//返给ajax请求
}
}
配置好web.xml中的servlet映射,这一步就略了。
3.建立ajax实现
这里为了快速实现用的是jquery实现的。
Java代码
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript">
function test(){
$.ajax({
type:"POST", //请求方式
url:"testjson", //请求路径
cache: false,
data:"name=zah", //传参
dataType: 'json', //返回值类型
success:function(json){
alert(json[0].username+" " json[0].password); //弹出返回过来的List对象
}
});
}
</script>
</head>
<body>
<input type="button" name="b" value="测试" οnclick=test()>
</body>
测试开始,点击按钮弹出zah 123
json[0]就相当于u1对象 json[1]相当于u2对象,对于其属性的访问跟java对象一样,其他的Connection测试应该一样,访问Map的话直接把返回函数改成如下即可:
Java代码
success:function(json){
alert(json.u1.username)
}
}直接跟上Key.属性即可访问Object对象。
项目源码在下面
from:http://hzjavaeyer.group.iteye.com/group/blog/692038
Java代码
package com.json;
public class User {
String username;
String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
再建一个servlet
Java代码
package com.json;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.json.JSONArray;
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");
String str= request.getParameter("name");//得到ajax传递过来的paramater
System.out.print(str);
PrintWriter out = 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 jo=JSONObject.fromObject(m);//转化Map对象
out.print(jsonArray2);//返给ajax请求
out.print(jo);//返给ajax请求
}
}
配置好web.xml中的servlet映射,这一步就略了。
3.建立ajax实现
这里为了快速实现用的是jquery实现的。
Java代码
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript">
function test(){
$.ajax({
type:"POST", //请求方式
url:"testjson", //请求路径
cache: false,
data:"name=zah", //传参
dataType: 'json', //返回值类型
success:function(json){
alert(json[0].username+" " json[0].password); //弹出返回过来的List对象
}
});
}
</script>
</head>
<body>
<input type="button" name="b" value="测试" οnclick=test()>
</body>
测试开始,点击按钮弹出zah 123
json[0]就相当于u1对象 json[1]相当于u2对象,对于其属性的访问跟java对象一样,其他的Connection测试应该一样,访问Map的话直接把返回函数改成如下即可:
Java代码
success:function(json){
alert(json.u1.username)
}
}直接跟上Key.属性即可访问Object对象。
项目源码在下面
from:http://hzjavaeyer.group.iteye.com/group/blog/692038