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
1.package com.json.domain;
2.
3.public class Person {
4. private int id;
5. private String name;
6. private String address;
7.
8. public Person() {
9. super();
10. }
11. public Person(int id, String name, String addrss) {
12. super();
13. this.id = id;
14. this.name = name;
15. this.address = addrss;
16. }
17. public int getId() {
18. return id;
19. }
20. public void setId(int id) {
21. this.id = id;
22. }
23. public String getName() {
24. return name;
25. }
26. public void setName(String name) {
27. this.name = name;
28. }
29. public String getAddress() {
30. return address;
31. }
32. public void setAddress(String address) {
33. this.address = address;
34. }
35. @Override
36. public String toString() {
37. return "Person [addrss=" + address + ", id=" + id + ", name=" + name
38. + "]";
39. }
40.
41.}
JsonService.java
1.package com.json.service;
2.
3.import java.util.ArrayList;
4.import java.util.HashMap;
5.import java.util.List;
6.import java.util.Map;
7.
8.import com.json.domain.Person;
9.
10.public class JsonService {
11.
12. public JsonService() {
13. }
14.
15. public Person getPerson(){
16. Person person = new Person(1001,"jack","上海黄浦区");
17. return person;
18. }
19. public List<Person> getListPerson(){
20. List<Person> list = new ArrayList<Person>();
21. Person person1 = new Person(1001,"jack","上海黄浦区");
22. Person person2 = new Person(1002,"rose","上海闵行区");
23. Person person3 = new Person(1003,"mick","上海黄浦区");
24. list.add(person1);
25. list.add(person2);
26. list.add(person3);
27. return list;
28. }
29.
30. public List<String> getListString(){
31. List<String> list = new ArrayList<String>();
32. list.add("北京");
33. list.add("上海");
34. list.add("湖南");
35. return list;
36. }
37.
38. public List<Map<String,Object>> getListMaps(){
39. List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
40. Map<String,Object> map1 = new HashMap<String, Object>();
41. Map<String,Object> map2 = new HashMap<String, Object>();
42. map1.put("id", 1001);
43. map1.put("name", "jack");
44. map1.put("address", "北京");
45. map2.put("id", 1001);
46. map2.put("name", "rose");
47. map2.put("address", "上海");
48. list.add(map1);
49. list.add(map2);
50. return list;
51. }
52.}
JsonServlet.java
1.package com.json.servlet;
2.
3.import java.io.IOException;
4.import java.io.PrintWriter;
5.
6.import javax.servlet.ServletException;
7.import javax.servlet.http.HttpServlet;
8.import javax.servlet.http.HttpServletRequest;
9.import javax.servlet.http.HttpServletResponse;
10.
11.import com.json.service.JsonService;
12.import com.json.tools.JsonTools;
13.
14.public class JsonServlet extends HttpServlet {
15. private JsonService service;
16. /**
17. * Constructor of the object.
18. */
19. public JsonServlet() {
20. super();
21. }
22.
23. /**
24. * Destruction of the servlet. <br>
25. */
26. public void destroy() {
27. super.destroy(); // Just puts "destroy" string in log
28. // Put your code here
29. }
30.
31. /**
32. * The doGet method of the servlet. <br>
33. *
34. * This method is called when a form has its tag value method equals to get.
35. *
36. * @param request the request send by the client to the server
37. * @param response the response send by the server to the client
38. * @throws ServletException if an error occurred
39. * @throws IOException if an error occurred
40. */
41. public void doGet(HttpServletRequest request, HttpServletResponse response)
42. throws ServletException, IOException {
43.
44. this.doPost(request, response);
45. }
46.
47. /**
48. * The doPost method of the servlet. <br>
49. *
50. * This method is called when a form has its tag value method equals to post.
51. *
52. * @param request the request send by the client to the server
53. * @param response the response send by the server to the client
54. * @throws ServletException if an error occurred
55. * @throws IOException if an error occurred
56. */
57. public void doPost(HttpServletRequest request, HttpServletResponse response)
58. throws ServletException, IOException {
59.
60. response.setContentType("text/html;charset=utf-8");
61. request.setCharacterEncoding("utf-8");
62. response.setCharacterEncoding("utf-8");
63. PrintWriter out = response.getWriter();
64.
65. String jsonString = "";
66. String action_flag = request.getParameter("action_flag");
67. if(action_flag.equals("person")){
68. jsonString = JsonTools.createJsonString("person", service.getPerson());
69. }else if(action_flag.equals("persons")){
70. jsonString = JsonTools.createJsonString("persons", service.getListPerson());
71. }else if(action_flag.equals("listString")){
72. jsonString = JsonTools.createJsonString("listString", service.getListString());
73. }else if(action_flag.equals("listMap")){
74. jsonString = JsonTools.createJsonString("listMap", service.getListMaps());
75. }
76. out.println(jsonString);
77. out.flush();
78. out.close();
79. }
80.
81. /**
82. * Initialization of the servlet. <br>
83. *
84. * @throws ServletException if an error occurs
85. */
86. public void init() throws ServletException {
87. service = new JsonService();
88. }
89.
90.}
web.xml中servlet映射
1.<servlet>
2. <description>This is the description of my J2EE component</description>
3. <display-name>This is the display name of my J2EE component</display-name>
4. <servlet-name>JsonServlet</servlet-name>
5. <servlet-class>com.json.servlet.JsonServlet</servlet-class>
6. </servlet>
7.
8. <servlet-mapping>
9. <servlet-name>JsonServlet</servlet-name>
10. <url-pattern>/servlet/JsonServlet</url-pattern>
11. </servlet-mapping>
我们通过浏览器
访问地址一: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
输出以下结果:
{"persons":[{"id":1001,"address":"北京","name":"jack"},{"id":1001,"address":"上海","name":"rose"}]}
本篇文章来源于 Linux公社网站(www.linuxidc.com) 原文链接:http://www.linuxidc.com/Linux/2013-03/80300.htm