1.Address类
package com.xuankai.model;
public class Address
{
private String homeAddress;
private String companyAddress;
public String getHomeAddress()
{
return homeAddress;
}
public void setHomeAddress(String homeAddress)
{
this.homeAddress = homeAddress;
}
public String getCompanyAddress()
{
return companyAddress;
}
public void setCompanyAddress(String companyAddress)
{
this.companyAddress = companyAddress;
}
}
2.people类
package com.xuankai.model;
import java.util.List;
public class People
{
private int id;
private String name;
private Address address;
private List<People> frends;
public int getId()
{
return id;
}
public void setId(int id)
{
this.id = id;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public Address getAddress()
{
return address;
}
public void setAddress(Address address)
{
this.address = address;
}
public List<People> getFrends()
{
return frends;
}
public void setFrends(List<People> frends)
{
this.frends = frends;
}
}
3.Servlet : JsonServlet
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 com.google.gson.Gson; //导入Gson
import com.xuankai.model.Address;
import com.xuankai.model.People;
public class JsonServlet extends HttpServlet
{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
List<People> list = new ArrayList<People>();
People people1 = new People();
people1.setId(1);
people1.setName("jack1");
Address address1 = new Address();
address1.setHomeAddress("beijing");
address1.setCompanyAddress("tianjing");
people1.setAddress(address1);
People people11 = new People();
people11.setId(4);
people11.setName("jack11");
People people12 = new People();
people12.setId(5);
people12.setName("jack12");
List<People> friends1 = new ArrayList<People>();
friends1.add(people11);
friends1.add(people12);
people1.setFrends(friends1);
System.out.println("*****************************");
People people2 = new People();
people2.setId(2);
people2.setName("tom2");
Address address2 = new Address();
address2.setHomeAddress("liaoning");
address2.setCompanyAddress("jilin");
people2.setAddress(address2);
People people21 = new People();
people21.setId(6);
people21.setName("tom21");
People people22 = new People();
people22.setId(7);
people22.setName("tom22");
List<People> friends2 = new ArrayList<People>();
friends2.add(people21);
friends2.add(people22);
people2.setFrends(friends2);
list.add(people1);
list.add(people2);
Gson gson = new Gson();
String result = gson.toJson(list);
System.out.println(result);
resp.setContentType("application/json;charset=utf-8");
resp.setHeader("pragma","no-cache");
resp.setHeader("cache-control","no-cache");
PrintWriter out = resp.getWriter();
out.println(result);
out.flush();
}
}
4.json.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<html>
<head>
<script type="text/javascript" src="scripts/jquery-1.7.2.js"></script> //导入jquery
<script type="text/javascript">
$(function(){
$("#button").click(function(){
$.get("JsonServlet",{},
function(returnedData,status){
var html = "<table width='60%' border='1' align='center'><tr><th>id</th><th>name</th><th>companyAddress</th><th>homeAddress</th><tr>";
for(var i=0;i<returnedData.length;i++)
{
var people = returnedData[i];
var id = people.id;
var name = people.name;
var companyAddress = people.address.companyAddress;
var homeAddress = people.address.homeAddress;
html += "<tr align='center'><td>"+id+"</td><td>"+name+"</td><td>"+companyAddress+"</td><td>"+homeAddress+"</td></tr>";
}
html += "</table>";
$("#body table:eq(0)").remove();
$("#body").append(html);
}
);
});
});
</script>
</head>
<body id="body">
<input type="button" value="get content from server" id="button">
</body>
</html>
运行结果为: