JSON数据格式的定义:
JSON的全称是:JavaScript Object Notation,是一种轻量级的数据交换格式。它构建于两种结构:
1、"名称/值" 对的集合(a collection of name / value pairs)。不同的语言中,它被理解为对象(Object),记录(Record),结构(struct),字典(Dictionary),哈希表(HashTable),有键列表(Keyed list),或者关联数组(Associative array)。
2、值的有序列表(An ordered list of values)。在大部分语言中,它被理解为数组(array)。
JSON数据格式的特点:
JSON对象时一个无序的" '名称/值' 对"的集合,一个对象以“{”(左括号)开始, 以"}"(右括号)结束,每个"名称"后跟一个":"(冒号),名称/值 对之间使用","(逗号)分隔。
如:
{
"name":"jackson",
"age":100
}
稍微复杂一点的情况,数组是值的有序集合,一个数组以“[”(左中括号)开始,以"]"(右中括号)结束。值之间使用","(逗号)分隔。
如:
{
"studengs":
[
{"name": "jackson", "age": 100},
{"name": "michael", "age": 51},
]
}
首先,我们搭建一个服务器的工程:JsonProject这个项目,工程目录结构:
源代码:
Person.java
package com.json.domain;
public class Person {
private int id;
private String name;
private String address;
public Person() {
super();
}
public Person(int id, String name, String addrss) {
super();
this.id=id;
this.name=name;
this.address=addrss;
}
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 String getAddress() {
return address;
}
public void setAddress(String address) {
this.address=address;
}
@Override
public String toString() {
return "Person [addrss=" + address + ",id=" + id + ",name=" + name
+ "]";
}
}
JsonService.java
package com.json.service;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.json.domain.Person;
public class JsonService {
public JsonService() {
}
public Person getPerson(){
Personperson=newPerson(1001,"jack","上海黄浦区");
return person;
}
public ListgetListPerson(){
Listlist=newArrayList();
Personperson1=newPerson(1001,"jack","上海黄浦区");
Personperson2=newPerson(1002,"rose","上海闵行区");
Personperson3=newPerson(1003,"mick","上海黄浦区");
list.add(person1);
list.add(person2);
list.add(person3);
return list;
}
public ListgetListString(){
Listlist=newArrayList();
list.add("北京");
list.add("上海");
list.add("湖南");
return list;
}
public List>getListMaps(){
List>list=newArrayList>();
Mapmap1=newHashMap();
Mapmap2=newHashMap();
map1.put("id", 1001);
map1.put("name", "jack");
map1.put("address", "北京");
map2.put("id", 1001);
map2.put("name", "rose");
map2.put("address", "上海");
list.add(map1);
list.add(map2);
return list;
}
}
JsonServlet.java
package com.json.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.json.service.JsonService;
import com.json.tools.JsonTools;
public class JsonServlet extends HttpServlet {
private JsonService service;
/**
* Constructor of the object.
*/
public JsonServlet() {
super();
}
/**
* Destruction of the servlet.
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* The doGet method of the servlet.
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request, response);
}
/**
* The doPost method of the servlet.
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
PrintWriterout=response.getWriter();
StringjsonString="";
Stringaction_flag=request.getParameter("action_flag");
if(action_flag.equals("person")){
jsonString=JsonTools.createJsonString("person", service.getPerson());
}else if(action_flag.equals("persons")){
jsonString=JsonTools.createJsonString("persons", service.getListPerson());
}else if(action_flag.equals("listString")){
jsonString=JsonTools.createJsonString("listString", service.getListString());
}else if(action_flag.equals("listMap")){
jsonString=JsonTools.createJsonString("listMap", service.getListMaps());
}
out.println(jsonString);
out.flush();
out.close();
}
/**
* Initialization of the servlet.
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
service=newJsonService();
}
}
web.xml中servlet映射
This is the description of my J2EE component
This is the display name of my J2EE component
JsonServlet
com.json.servlet.JsonServlet
JsonServlet
/servlet/JsonServlet
我们通过浏览器
访问地址一:http://wulianghuan-pc:8080/JsonProject/servlet/JsonServlet?action_flag=person
输出以下结果:
{"persons":{"address":"上海黄浦区","id":1001,"name":"jack"}}
访问地址二:http://wulianghuan-pc:8080/JsonProject/servlet/JsonServlet?action_flag=persons
输出以下结果:
{"persons":[{"address":"上海黄浦区","id":1001,"name":"jack"},{"addrss":"上海闵行区","id":1002,"name":"rose"},{"address":"上海黄浦区","id":1003,"name":"mick"}]}
访问地址三:http://wulianghuan-pc:8080/JsonProject/servlet/JsonServlet?action_flag=listString
输出以下结果:
{"persons":["北京","上海","湖南"]}
访问地址四:http://wulianghuan-pc:8080/JsonProject/servlet/JsonServlet?action_flag=listMap