JSON(JavaScript Object Notation)
首先來看一下JSON官方對於“JSON”的解釋:
JSON(JavaScript Object Notation) 是一種輕量級的數據交換格式。易於人閱讀和編寫。同時也易於機器解析和生成。它基於JavaScript Programming Language, Standard ECMA-262 3rd Edition - December 1999的一個子集。 JSON採用完全獨立於語言的文本格式,但是也使用了類似於C語言家族的習慣(包括C, C++, C#, Java, JavaScript, Perl, Python等)。這些特性使JSON成爲理想的數據交換語言。(更多內容請參見JSON官網http://json.org/json-zh.html)
JSON建構於兩種結構:
“名稱/值”對的集合(A collection of name/value pairs)。不同的語言中,它被理解爲對象(object),紀錄(record),結構(struct),字典(dictionary),哈希表(hash table),有鍵列表(keyed list),或者關聯數組 (associative array)。
值的有序列表(An ordered list of values)。在大部分語言中,它被理解爲數組(array)。
因爲JSON中的值(value)可以是雙引號括起來的字符串(string)、數值(number)、true、false、 null、對象(object)或者數組(array),且這些結構可以嵌套,這種特性給予JSON表達數據以無限的可能:它既可以表達一個簡單的 key/value,也可以表達一個複雜的Map或List,而且它是易於閱讀和理解的。
Struts2中JSON的用武之地
因爲JSON是脫離語言的理想的數據交換格式,所以它被頻繁的應用在客戶端與服務器的通信過程中,這一點是毋庸置疑的。而在客戶端與服務器的通信過程中,JSON數據的傳遞又被分爲服務器向客戶端傳送JSON數據,和客戶端向服務器傳送JSON數據,前者的核心過程中將對象轉換成JSON,而後者的核心是將JSON轉換成對象,這是本質的區別。另外,值得一提的是,JSON數據在傳遞過程中,其實就是傳遞一個普通的符合JSON語法格式的字符串而已,所謂的“JSON對象”是指對這個JSON字符串解析和包裝後的結果,這一點請牢記,因爲下面的內容會依賴這一點。
Struts2返回JSON數據到客戶端
這是最常見的需求,在AJAX大行其道的今天,向服務器請求JSON數據已成爲每一個WEB應用必備的功能。拋開Struts2暫且不提,在常規WEB應用中由服務器返回JSON數據到客戶端有兩種方式:一是在Servlet中輸出JSON串,二是在JSP頁面中輸出JSON串。上文提到,服務器像客戶端返回JSON數據,其實就是返回一個符合JSON語法規範的字符串,所以在上述兩種 方法中存在一個共同點,就是將需要返回的數據包裝稱符合JSON語法規範的字符串後在頁面中顯示。如下所示
使用Servlet返回JSON數據到客戶端:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
|
package
cn.ysh.studio.struts2.json.demo.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
net.sf.json.JSONObject; import
cn.ysh.studio.struts2.json.demo.bean.User; public
class
JSON extends
HttpServlet { /** * */ private
static
final
long
serialVersionUID = 1L; /** *
The doGet method of the servlet. <br> * *
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 { response.setContentType( "text/html" ); PrintWriter
out = response.getWriter(); //將要被返回到客戶端的對象 User
user= new
User(); user.setId( "123" ); user.setName( "JSONServlet" ); user.setPassword( "JSON" ); user.setSay( "Hello
, i am a servlet !" ); JSONObject
json= new
JSONObject(); json.accumulate( "success" , true ); json.accumulate( "user" ,
user); out.println(json.toString()); //
因爲JSON數據在傳遞過程中是以普通字符串形式傳遞的,所以我們也可以手動拼接符合JSON語法規範的字符串輸出到客戶端 //
以下這兩句的作用與38-46行代碼的作用是一樣的,將向客戶端返回一個User對象,和一個success字段 //
String jsonString="{\"user\":{\"id\":\"123\",\"name\":\"JSONServlet\",\"say\":\"Hello , i am a servlet !\",\"password\":\"JSON\"},\"success\":true}"; //
out.println(jsonString); out.flush(); out.close(); } /** *
The doPost method of the servlet. <br> * *
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 { doGet(request,
response); } } |
結果在意料之中,如下圖所示:
使用JSP(或html等)返回JSON數據到客戶端:
1
2
|
<%@
page language="java" import="java.util.*" pageEncoding="UTF-8"%> {"user":{"id":"123","name":"JSONJSP","say":"Hello
, i am a JSP !","password":"JSON"},"success":true} |
這個就不用去看結果了吧。
再回到Struts,在Struts的MVC模型中,Action替代Servlet擔當了Model的角色,所以對於Struts而言,返回 JSON數據到客戶端,跟傳統的WEB應用一樣,存在兩種方式,即在Action中輸出JSON數據,和在視圖資源中輸出JSON數據。再往下細分的話,在Action中輸出JSON數據又分爲兩種方式,一是使用傳統方式輸出自己包裝後的JSON數據,二是使用Struts自帶的JSON數據封裝功能來自動包裝並返回JSON數據。
在視圖資源中輸出JSON數據
Action處理完用戶請求後,將數據存放在某一位置,如request中,並返回視圖,然後Struts將跳轉至該視圖資源,在該視圖中,我們需要做的是將數據從存放位置中取出,然後將其轉換爲JSON字符串,輸出在視圖中。這跟傳統WEB應用中在JSP頁面輸出JSON數據的做法如出一轍:
1
2
3
4
5
6
7
8
9
10
11
12
|
public
String testByJSP() { User
user = new
User(); user.setId( "123" ); user.setName( "Struts2" ); user.setPassword( "123" ); user.setSay( "Hello
world !" ); JSONObject
jsonObject= new
JSONObject(); jsonObject.accumulate( "user" ,
user); //這裏在request對象中放了一個data,所以struts的result配置中不能有type="redirect" ServletActionContext.getRequest().setAttribute( "data" ,
jsonObject.toString()); return
SUCCESS; }; |
因爲是常規的Struts流程配置,所以配置內容就不再展示了。
JSP代碼就非常簡單了,
1
2
|
<%@
page language="java" import="java.util.*" pageEncoding="UTF-8"%> ${data
} |
結果如圖所示:
在Action中以傳統方式輸出JSON數據
這一點跟傳統的Servlet的處理方式基本上一模一樣,代碼如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
public
void
doAction() throws
IOException{ HttpServletResponse
response=ServletActionContext.getResponse(); //以下代碼從JSON.java中拷過來的 response.setContentType( "text/html" ); PrintWriter
out; out
= response.getWriter(); //將要被返回到客戶端的對象 User
user= new
User(); user.setId( "123" ); user.setName( "JSONActionGeneral" ); user.setPassword( "JSON" ); user.setSay( "Hello
, i am a action to print a json!" ); JSONObject
json= new
JSONObject(); json.accumulate( "success" , true ); json.accumulate( "user" ,
user); out.println(json.toString()); //
因爲JSON數據在傳遞過程中是以普通字符串形式傳遞的,所以我們也可以手動拼接符合JSON語法規範的字符串輸出到客戶端 //
以下這兩句的作用與38-46行代碼的作用是一樣的,將向客戶端返回一個User對象,和一個success字段 //
String jsonString="{\"user\":{\"id\":\"123\",\"name\":\"JSONActionGeneral\",\"say\":\"Hello , i am a action to print a json!\",\"password\":\"JSON\"},\"success\":true}"; //
out.println(jsonString); out.flush(); out.close(); } |
1
2
3
4
|
< package
name = "default"
extends = "struts-default"
namespace = "/" > < action
name = "testJSONFromActionByGeneral"
class = "cn.ysh.studio.struts2.json.demo.action.UserAction"
method = "doAction" > </ action > </ package > |
注意:這個action沒有result,且doAction方法沒有返回值!
就不再貼圖了,因爲結果可想而知!
在Action中以Struts2的方式輸出JSON數據
本着“不重複發明輪子”的原則,我們將轉換JSON數據的工作交給Struts2來做,那麼相對於在Action中以傳統方式輸出JSON不同的是,Action是需要將注意力放在業務處理上,而無需關心處理結果是如何被轉換成JSON被返回客戶端的——這些 工作通過簡單的配置,Struts2會幫我們做的更好。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
public
String testByAction() { //
dataMap中的數據將會被Struts2轉換成JSON字符串,所以這裏要先清空其中的數據 dataMap.clear(); User
user = new
User(); user.setId( "123" ); user.setName( "JSONActionStruts2" ); user.setPassword( "123" ); user.setSay( "Hello
world !" ); dataMap.put( "user" ,
user); //
放入一個是否操作成功的標識 dataMap.put( "success" , true ); //
返回結果 return
SUCCESS; } |
1
2
3
4
5
6
7
8
9
|
< package
name = "json"
extends = "json-default"
namespace = "/test" > < action
name = "testByAction" class = "cn.ysh.studio.struts2.json.demo.action.UserAction"
method = "testByAction" > < result
type = "json" > <!--
這裏指定將被Struts2序列化的屬性,該屬性在action中必須有對應的getter方法 --> < param
name = "root" >dataMap</ param > </ result > </ action > </ package > |
凡是使用Struts2序列化對象到JSON的action,所在的package必須繼承自json-default,注意,這裏唯一的result,沒有指定name屬性。
結果如下圖所示:
上面很詳細的說明了在WEB應用中如何返回JSON數據到客戶端,講了那麼多種方式,涉及的技術核心無非只有兩點:
1、將對象轉換成符合JSON語法格式的字符串;
2、將符合JSON語法格式的字符串返回客戶端;
第二點是整個實現過程的本質,但卻不難做到;第一點其實也不難,他甚至有兩種做法,一是通過字符串拼接方式,而是通過JSONObject以對象方式轉換。看下面的一個例子:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
package
cn.ysh.studio.struts2.json.demo.test; import
cn.ysh.studio.struts2.json.demo.bean.User; import
net.sf.json.JSONObject; public
class
JSONTest { /** *
將普通的pojo轉換成JSON字符串 *
@return */ public
JSONObject bean2json() { User
user = new
User(); user.setId( "JSONTest" ); user.setName( "JSONTest" ); user.setPassword( "JSON" ); user.setSay( "Hello,i
am JSONTest.java" ); JSONObject
jsonObject = new
JSONObject(); jsonObject.accumulate( "user" ,
user); System.out.println( "User轉換後的字符串:" +jsonObject.toString()); return
jsonObject; } /** *
從JSONObject對象中反向解析出User對象 *
@param jsonObject */ public
void
json2bean(JSONObject jsonObject) { User
user=(User)JSONObject.toBean((JSONObject)jsonObject.get( "user" ),User. class ); System.out.println( "轉換得到的User對象的Name爲:" +user.getName()); } public
static
void
main(String[] s) { JSONTest
tester= new
JSONTest(); tester.json2bean(tester.bean2json()); } } |
JSON格式的字符串返回到客戶端後,客戶端會將其解析並封裝成真正的JSON對象,以供JS調用。
總結上述,其實只要明白了服務器返回JSON數據到客戶端的原理,做起來就遊刃有餘了,他甚至有非常多的可選方案,但既然是基於 Struts2的實現,那麼肯定還是要用Struts2的方式來做啦,因爲這樣確實可以省很多事。另外,在文章的最後,說明一下返回JSON數據時在 result中配置的參數的含義及其常見常見配置吧:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
< result
type = "json" > <!--
這裏指定將被Struts2序列化的屬性,該屬性在action中必須有對應的getter方法 --> <!--
默認將會序列所有有返回值的getter方法的值,而無論該方法是否有對應屬性 --> < param
name = "root" >dataMap</ param > <!--
指定是否序列化空的屬性 --> < param
name = "excludeNullProperties" >true</ param > <!--
這裏指定將序列化dataMap中的那些屬性 --> < param
name = "includeProperties" > userList.* </ param > <!--
這裏指定將要從dataMap中排除那些屬性,這些排除的屬性將不被序列化,一半不與上邊的參數配置同時出現 --> < param
name = "excludeProperties" > SUCCESS </ param > </ result > |
值得一提的是通過Struts2來返回JSON數據,在IE中會提示下載,這個不用關心,換個瀏覽器就能正常展示JSON數據,而在JS調用中,更是毫無影響。
下面是整個Action的完整代碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
|
package
cn.ysh.studio.struts2.json.demo.action; import
java.io.IOException; import
java.io.PrintWriter; import
java.util.HashMap; import
java.util.Map; import
javax.servlet.http.HttpServletResponse; import
org.apache.struts2.ServletActionContext; import
net.sf.json.JSONObject; import
cn.ysh.studio.struts2.json.demo.bean.User; import
com.opensymphony.xwork2.ActionSupport; public
class
UserAction extends
ActionSupport { /** * */ private
static
final
long
serialVersionUID = 1L; //將會被Struts2序列化爲JSON字符串的對象 private
Map<String, Object> dataMap; /** *
構造方法 */ public
UserAction() { //初始化Map對象 dataMap
= new
HashMap<String, Object>(); } /** *
測試通過action以視圖方式返回JSON數據 *
@return */ public
String testByJSP() { User
user = new
User(); user.setId( "123" ); user.setName( "JSONActionJSP" ); user.setPassword( "123" ); user.setSay( "Hello
world !" ); JSONObject
jsonObject= new
JSONObject(); jsonObject.accumulate( "user" ,
user); jsonObject.accumulate( "success" , true ); //這裏在request對象中放了一個data,所以struts的result配置中不能有type="redirect" ServletActionContext.getRequest().setAttribute( "data" ,
jsonObject.toString()); return
SUCCESS; }; /** *
測試通過action以Struts2默認方式返回JSON數據 *
@return */ public
String testByAction() { //
dataMap中的數據將會被Struts2轉換成JSON字符串,所以這裏要先清空其中的數據 dataMap.clear(); User
user = new
User(); user.setId( "123" ); user.setName( "JSONActionStruts2" ); user.setPassword( "123" ); user.setSay( "Hello
world !" ); dataMap.put( "user" ,
user); //
放入一個是否操作成功的標識 dataMap.put( "success" , true ); //
返回結果 return
SUCCESS; } /** *
通過action是以傳統方式返回JSON數據 *
@throws IOException */ public
void
doAction() throws
IOException{ HttpServletResponse
response=ServletActionContext.getResponse(); //以下代碼從JSON.java中拷過來的 response.setContentType( "text/html" ); PrintWriter
out; out
= response.getWriter(); //將要被返回到客戶端的對象 User
user= new
User(); user.setId( "123" ); user.setName( "JSONActionGeneral" ); user.setPassword( "JSON" ); user.setSay( "Hello
, i am a action to print a json!" ); JSONObject
json= new
JSONObject(); json.accumulate( "success" , true ); json.accumulate( "user" ,
user); out.println(json.toString()); //
因爲JSON數據在傳遞過程中是以普通字符串形式傳遞的,所以我們也可以手動拼接符合JSON語法規範的字符串輸出到客戶端 //
以下這兩句的作用與38-46行代碼的作用是一樣的,將向客戶端返回一個User對象,和一個success字段 //
String jsonString="{\"user\":{\"id\":\"123\",\"name\":\"JSONActionGeneral\",\"say\":\"Hello , i am a action to print a json!\",\"password\":\"JSON\"},\"success\":true}"; //
out.println(jsonString); out.flush(); out.close(); } /** *
Struts2序列化指定屬性時,必須有該屬性的getter方法,實際上,如果沒有屬性,而只有getter方法也是可以的 *
@return */ public
Map<String, Object> getDataMap() { return
dataMap; } } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
<? xml
version = "1.0"
encoding = "UTF-8" ?> <!DOCTYPE
struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> < struts > < package
name = "json"
extends = "json-default"
namespace = "/test" > < action
name = "testByAction" class = "cn.ysh.studio.struts2.json.demo.action.UserAction"
method = "testByAction" > < result
type = "json" > <!--
這裏指定將被Struts2序列化的屬性,該屬性在action中必須有對應的getter方法 --> <!--
默認將會序列所有有返回值的getter方法的值,而無論該方法是否有對應屬性 --> < param
name = "root" >dataMap</ param > <!--
指定是否序列化空的屬性 --> <!-- <param
name="excludeNullProperties">true</param> --> <!--
這裏指定將序列化dataMap中的那些屬性 --> <!-- <param
name="includeProperties"> userList.* </param> -->
<!--指定內容類型,默認爲application/json,IE瀏覽器會提示下載--> <!-- <!--
這裏指定將要從dataMap中排除那些屬性,這些排除的屬性將不被序列化,一半不與上邊的參數配置同時出現 --> <!-- <param
name="excludeProperties"> SUCCESS </param> --> </ result > </ action > </ package > < package
name = "default"
extends = "struts-default"
namespace = "/" > < action
name = "testJSONFromActionByGeneral" class = "cn.ysh.studio.struts2.json.demo.action.UserAction"
method = "doAction" > </ action > < action
name = "testByJSP" class = "cn.ysh.studio.struts2.json.demo.action.UserAction"
method = "testByJSP" > < result
name = "success" >/actionJSP.jsp</ result > </ action > </ package > </ struts > |