最近公司開(kāi)發(fā)的幾個(gè)項(xiàng)目中,后臺(tái)Action向前端傳遞數(shù)據(jù)都是Json格式,于是對(duì)JSONObject、JSONArray簡(jiǎn)單的研究了一下,廢話(huà)不多說(shuō),想使用JSONObject、JSONArray,策則在項(xiàng)目中必須要有commons-lang.jar commons-beanutils.jar commons-collections.jar commons-logging.jar ezmorph.jar json-lib-2.2.2-jdk15.jar 這些Jar包。
1.JSONObject與JSONArray使用的場(chǎng)景區(qū)別;
- 想通過(guò)鍵值對(duì)的形式獲取數(shù)據(jù),使用JSONObject。
- 如果后臺(tái)查詢(xún)的是某個(gè)bean的list集合向前端頁(yè)面?zhèn)鬟f,使用JSONArray。
2. JSONObject與JSONArray使用方法區(qū)別;
創(chuàng)建方法不同:
JSONObject創(chuàng)建的方法:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
//創(chuàng)建JsonObject第一種方法 JSONObject jsonObject = new JSONObject(); jsonObject.put( "UserName" , "kobi" ); jsonObject.put( "age" , "34" ); jsonObject.put( "workIn" , "ALI" ); System.out.println( "jsonObject1:" + jsonObject); //創(chuàng)建JsonObject第二種方法 HashMap<String, String> hashMap = new HashMap<String, String>(); hashMap.put( "UserName" , "ZHULI" ); hashMap.put( "age" , "30" ); hashMap.put( "workIn" , "ALI" ); System.out.println( "jsonObject2:" + JSONObject.fromObject(hashMap)); |
JSONArray創(chuàng)建的方法
1
2
3
4
5
6
7
8
9
10
11
12
13
|
//創(chuàng)建一個(gè)JsonArray方法1 JSONArray jsonArray = new JSONArray(); jsonArray.add( 0 , "kobi" ); jsonArray.add( 1 , "34" ); jsonArray.add( 2 , "ALI" ); System.out.println( "jsonArray1:" + jsonArray); //創(chuàng)建JsonArray方法2 ArrayList<String> arrayList = new ArrayList<String>(); arrayList.add( "kobi" ); arrayList.add( "34" ); arrayList.add( "ALI" ); System.out.println( "jsonArray2:" + JSONArray.fromObject(arrayList)); |
獲取方式不同
- 獲取JSONObject中值:String userName = jsonObject.getString("UserName");
- 獲取JSONArray中的值:String userName = arrayList.getString("2");
示例
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
|
package com.yunos.tv.video.resource.controller.web; import java.util.ArrayList; import java.util.HashMap; import net.sf.json.JSONArray; import net.sf.json.JSONObject; public class Test { public static void main(String[] args) { //JsonObject和JsonArray區(qū)別就是JsonObject是對(duì)象形式,JsonArray是數(shù)組形式 //創(chuàng)建JsonObject第一種方法 JSONObject jsonObject = new JSONObject(); jsonObject.put( "UserName" , "ZHULI" ); jsonObject.put( "age" , "30" ); jsonObject.put( "workIn" , "ALI" ); System.out.println( "jsonObject1:" + jsonObject); //創(chuàng)建JsonObject第二種方法 HashMap<String, String> hashMap = new HashMap<String, String>(); hashMap.put( "UserName" , "ZHULI" ); hashMap.put( "age" , "30" ); hashMap.put( "workIn" , "ALI" ); System.out.println( "jsonObject2:" + JSONObject.fromObject(hashMap)); //創(chuàng)建一個(gè)JsonArray方法1 JSONArray jsonArray = new JSONArray(); jsonArray.add( 0 , "ZHULI" ); jsonArray.add( 1 , "30" ); jsonArray.add( 2 , "ALI" ); System.out.println( "jsonArray1:" + jsonArray); //創(chuàng)建JsonArray方法2 ArrayList<String> arrayList = new ArrayList<String>(); arrayList.add( "ZHULI" ); arrayList.add( "30" ); arrayList.add( "ALI" ); System.out.println( "jsonArray2:" + JSONArray.fromObject(arrayList)); //如果JSONArray解析一個(gè)HashMap,則會(huì)將整個(gè)對(duì)象的放進(jìn)一個(gè)數(shù)組的值中 System.out.println( "jsonArray FROM HASHMAP:" + JSONArray.fromObject(hashMap)); //組裝一個(gè)復(fù)雜的JSONArray JSONObject jsonObject2 = new JSONObject(); jsonObject2.put( "UserName" , "ZHULI" ); jsonObject2.put( "age" , "30" ); jsonObject2.put( "workIn" , "ALI" ); jsonObject2.element( "Array" , arrayList); System.out.println( "jsonObject2:" + jsonObject2); } } |
結(jié)果:
jsonObject1:{"UserName":"ZHULI","age":"30","workIn":"ALI"}
jsonObject2:{"workIn":"ALI","age":"30","UserName":"ZHULI"}
jsonArray1:["ZHULI","30","ALI"]
jsonArray2:["ZHULI","30","ALI"]
jsonArray FROM HASHMAP:[{"workIn":"ALI","age":"30","UserName":"ZHULI"}]
jsonObject2:{"UserName":"ZHULI","age":"30","workIn":"ALI","Array":["ZHULI","30","ALI"]}
3. 解析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
|
package com.yunos.tv.video.resource.controller.web; import net.sf.json.JSONArray; import net.sf.json.JSONObject; public class Test { public static void main(String[] args) { String jsonString = "{\"UserName\":\"kobi\",\"age\":\"34\",\"workIn\":\"ALI\",\"Array\":[\"kobi\",\"34\",\"ALI\"]}" ; //將Json字符串轉(zhuǎn)為java對(duì)象 JSONObject obj = JSONObject.fromObject(jsonString); //獲取Object中的UserName if (obj.has( "UserName" )) { System.out.println( "UserName:" + obj.getString( "UserName" )); } //獲取ArrayObject if (obj.has( "Array" )) { JSONArray transitListArray = obj.getJSONArray( "Array" ); for ( int i = 0 ; i < transitListArray.size(); i++) { System.out.print( "Array:" + transitListArray.getString(i) + " " ); } } } } |
返回值:
UserName:kobi
Array:kobi Array:34 Array:ALI
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:http://www.cnblogs.com/snake-hand/p/3167787.html