json.jsp
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'json.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<script type="text/javascript" src="scripts/jquery-1.3.1.js"/>
<script type="text/javascript">
$(function()
{
$("#button1").click(function
{
$.get("GsonServlet",{},function(returnedData,status)
{
//
var html="<table width='60%' border='1' align='center'><tr><th>id</th><th>name</th><th>compayAddress</th><th>sssshomeAddress</th>";
for(int i=0;i<returnedData.length;i++){
var people=returnedData[i]
var id=people.id;
var name=people.name;
var companyAddress=people.companyAddress;
var homeAddress=people.homeAddress;
html+="<tr align='center'><td>"+id+"</td><td>"+name+"</td><td>"+companyAddress+"</td><td>"+homeAddress+"</td>"
html+="</table>";
}
$("#theBody table:eq(0)").remove();
$("#theBody").append(html);
})
})
})
</script>
</head>
<body>
<input type="button" value="get json content from server" id="button1"></input>
</body>
</html>
Address.java
package com.sun.model;
public class Address {
private String homeAddress;
private String compayAddress;
public String getHomeAddress() {
return homeAddress;
}
public void setHomeAddress(String homeAddress) {
this.homeAddress = homeAddress;
}
public String getCompayAddress() {
return compayAddress;
}
public void setCompayAddress(String compayAddress) {
this.compayAddress = compayAddress;
}
}
People .java
package com.sun.model;
import java.util.List;
public class People {
private int id;
private String name;
private Address address;
private List<People> friends;
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> getFriends() {
return friends;
}
public void setFriends(List<People> friends) {
this.friends = friends;
}
}
GsonServlet .java
package com.sun.servlet;
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;
import com.sun.model.Address;
import com.sun.model.People;
public class GsonServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
List<People> list = new ArrayList<People>();
//构造第一个人
People people1 = new People();//对象类型属性
Address address1 = new Address();//对象类型属性
address1.setCompayAddress("beijing");
address1.setHomeAddress("shanghai");
people1.setId(1);
people1.setName("zhangsan");
people1.setAddress(address1);
//朋友
//第一个人
People people11 = new People();
people11.setId(5);
people11.setName("zhangsan1");
//第二个人
People people12 = new People();
people12.setId(6);
people12.setName("zhangsan2");
List<People> list1 = new ArrayList<People>();
list.add(people11);
list.add(people12);
people1.setFriends(list);
System.out.println("--------------");
//构造第二个人
People people2=new People();
Address address2=new Address();
address2.setCompayAddress("liaoning");
address2.setHomeAddress("吉林");
people2.setId(2);
people2.setName("lisi");
people2.setAddress(address2);
List<People> list2=new ArrayList<People>();
People people21=new People();
people21.setId(3);
people21.setName("lisi11");
People people22=new People();
people22.setId(4);
people22.setName("lisi12");
list2.add(people21);
list2.add(people22);
people2.setFriends(list2);
list.add(people1);
list.add(people2);
Gson gson=new Gson();
String result=gson.toJson(list);
System.out.println(result);
}
}