阿里巴巴的fastjson 之JSONObject實際使用以及JSONArray與JSONObject互換


一:最近有一個大部分 json 處理的項目,以下是項目中實際使用的進行總結,當然肯定也有參考網上不少優秀的博主的文章:

#1:Java 對象與 JSON字符串 相互轉換
    parseObject(): 則反過來將 JSON 字符串轉換成java對象。 eg:

 1         User user1 = new User();
 2         user1.setPhone("17312655215");
 3         user1.setUserName("王一博");
 4         String user = com.alibaba.fastjson.JSONObject.toJSONString(user1);
 5         User user2 = com.alibaba.fastjson.JSONObject.parseObject(user, User.class);
 6         System.out.println("獲取到的user2: " + user2);
 7         //result: 獲取到的user2: User(userId=null,userName=王一博, phone=17312655215)
 8         //示例2
 9         Map<String, String> hashMap = new HashMap<>();
10         hashMap.put("1", "王大寶");
11         hashMap.put("2", "李二寶");
12         hashMap.put("3", "張小寶");
13         String hashMapJson = JSONObject.toJSONString(hashMap);
14         HashMap hashMap1 = JSONObject.parseObject(hashMapJson, HashMap.class);
15         System.out.println("hashMap: " + hashMap1);
16         //result: hashMap: {1=王大寶, 2=李二寶, 3=張小寶}
View Code

    toJSONString(): 可將java對象轉換成 JSON 字符串。eg:

 1         User user1 = new User();
 2         user1.setPhone("17312655215");
 3         user1.setUserName("王一博");
 4         String user = com.alibaba.fastjson.JSONObject.toJSONString(user1);
 5         //result:jsonobject:{"1":"王一博","2":"王二博","3":"王三博"}
 6 示例2:
 7         Map<String, String> hashMap = new HashMap<>();
 8         hashMap.put("1", "王大寶");
 9         hashMap.put("2", "李二寶");
10         hashMap.put("3", "張小寶");
11         String hashMapJson = JSONObject.toJSONString(hashMap);
12         System.out.println("hashMap實驗:" + hashMapJson);
13         //result: hashMap實驗:{"1":"王大寶","2":"李二寶","3":"張小寶"}
View Code

#2:存值put(String,Object) 、根據key刪除元素remove()。eg

 1         JSONObject jsonObject = new JSONObject();
 2         jsonObject.put("1", "王一博");
 3         jsonObject.put("2", "王二博");
 4         jsonObject.put("3", "王三博");
 5         System.out.println("jsonobject:" + jsonObject);
 6         //result:jsonobject:{"1":"王一博","2":"王二博","3":"王三博"}
 7         
 8         System.out.println("移除前的數據:jsonObject " + jsonObject);
 9         //result: 移除前的數據:jsonObject {"1":"王一博","2":"王一博","3":"王三博","4":"four博"}
10         Object remove = jsonObject.remove("1");
11         System.out.println("移除的數據:remove" + remove);
12         //result: 移除的數據:remove王一博
13         System.out.println("移除后的數據:jsonObject " + jsonObject);
14         //result: jsonObject {"2":"王一博","3":"王三博","4":"four博"}
View Code

 #3:獲取值、查詢個數、判斷JSONobject對象是否為空、是否包含key、是否包含value

 1         JSONObject jsonObject = new JSONObject();
 2         jsonObject.put("1", "王一博");
 3         jsonObject.put("2", "王一博");
 4         jsonObject.put("3", "王三博");
 5         jsonObject.put("4", "four博");
 6         String userName = jsonObject.getString("1");
 7         System.out.println("userName:" + userName);
 8         //result: userName:王一博
 9 
10         int size = jsonObject.size();
11         System.out.println("個數:" + size);
12         //result:個數:4
13 
14         boolean isEmpty = jsonObject.isEmpty();
15         System.out.println("isEmpty():" + isEmpty);
16         //result: isEmpty():false
17 
18         boolean isContainsKey = jsonObject.containsKey("1");
19         System.out.println("isContainsKey():" + isContainsKey);
20         //result: isContainsKey():true
21 
22         boolean isContainsValue = jsonObject.containsValue("王一博");
23         System.out.println("isContainsValue():" + isContainsValue);
View Code

 #4: keySet(): 獲取對象中的所有key。entrySet() : 獲取對象中所有鍵值對。

 1         JSONObject jsonObject = new JSONObject();
 2         jsonObject.put("1", "王一博");
 3         jsonObject.put("2", "王一博");
 4         jsonObject.put("3", "王三博");
 5         jsonObject.put("4", "four博");
 6 
 7         //keySet:獲取對象中所有的key
 8         Set<String> strings = jsonObject.keySet();
 9         for (String key : strings) {
10             System.out.print("   " + key);
11         }
12         //result:    1   2   3   4
13         System.out.println();
14         //entrySet():在循環遍歷時使用,取得是鍵和值的映射關系,Entry就是Map接口中的內部接口 與String字符串轉換:
15         Set<Map.Entry<String, Object>> entrySet = jsonObject.entrySet();
16         for (Map.Entry<String, Object> entry : entrySet) {
17             System.out.println(entry);
18         }
19         //result : 1=王一博、2=王一博、3=王三博、4=four博
View Code
#5:JSONObject 中包含 JSONObject 對象,或者包含JSONArray對象
 1         //8.如果JSONObject對象中的value是一個JSONObject數組,既根據key獲取對應的JSONObject數組;
 2         JSONObject objectArray = new JSONObject();
 3         //創建JSONArray數組
 4         JSONArray jsonArray = new JSONArray();
 5         //在JSONArray數組設值:jsonArray.add(int index, Object value);
 6         jsonArray.add(0, "this is a jsonArray value");
 7         jsonArray.add(1, "another jsonArray value");
 8         objectArray.put("testArray", jsonArray);
 9         //獲取JSONObject對象中的JSONArray數組
10         JSONArray jsonArray2 = objectArray.getJSONArray("testArray");
11         System.out.println(jsonArray2);
12 
13         //9: JSONObjct對象中的value是一個JSONObject對象,即根據key獲取對應的JSONObject對象;
14         JSONObject json1 = new JSONObject();
15         JSONObject user1 = new JSONObject();
16         user1.put("1", "17312655215");
17         user1.put("2", "王一博");
18         json1.put("test", user1);
19         System.out.println("JSONObjct:" + json1);
20         String test = json1.getString("test");
21         System.out.println("test:" + test);
22         //result: JSONObjct:{"test":{"1":"17312655215","2":"王一博"}}
View Code
#6: toJavaObject(): 將JSON 數據類型轉 java 數據類型
 1       JSONObject jsonObject = new JSONObject();
 2         jsonObject.put("1", "今天是2021.12.5");
 3         jsonObject.put("2", "今天是2021.12.6");
 4         jsonObject.put("3", "今天是2021.12.7");
 5         jsonObject.put("4", "今天是2021.12.8");
 6 
 7 
 8         //toJavaObject(JSON json, Class<T> clazz);
 9         Map map = JSONObject.toJavaObject(jsonObject, Map.class);
10         System.out.println("map是:" +map);
11         //result: map是:{"1":"今天是2021.12.5","2":"今天是2021.12.6","3":"今天是2021.12.7","4":"今天是2021.12.8"}
View Code

 #二:JSONObject和JSONArray的數據表示形式

#1: JSONObject的數據是用  { }  來表示。都是以 key-value 形式出現的Map結構

    eg: { "id" : "123", "courseID" : "huangt-test", "title" : "提交作業", "content" : null }

 

#2:JSONArray是用  [ { } , { } , ...... , { } ]  來表示。都是以 json 形式出現的 List 數組結構

    eg: [ { "id" : "123", "courseID" : "huangt-test", "title" : "提交作業" } , { "content" : null, "beginTime" : 1398873600000 "endTime" } ] ;

    總結:java 中的 List 或者 Map 分別對應的是 JSONArray 和 JSONObject。

 

#3 JSONOject和JSONArray()方法如何互換

 JSONObject對象的getJSONArray(key)方法獲取到一個JSONArray對象,再調用JSONArray對象的get(i)方法獲取數組元素,i為索引值。
#4:JSONArray 中常用的方法。
1         //將字符串轉JSONArray 類型
2         JSONArray objects = JSONArray.parseArray("");
3         //將字符串轉其他 類型
4         List<List> lists = JSONArray.parseArray("", List.class);
View Code
 

 



免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM