FastJson中JSONObject用法及常用方法總結
本文為博主原創,未經允許不得轉載:
最近一直有用到解析各種數據,主要是用FastJson進行數據解析,其中一個重要的類為JSONObject,今天有時間,所以進行總結一下:
JSONobject是FastJson提供的對象,在api中是用一個私有的常量map進行封裝的,實際就是一個map,只不過FastJson對其進行了封裝,
添加了很多方便快捷的屬性方法。
final Map<String, Object> map;
在項目中添加maven依賴
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.48</version>
</dependency>
先來看下它有哪些常用方法,以及有什麼作用:
1.put(String key, Object value)方法,在JSONObject對像中設置鍵值對在,在進行設值得時候,key是唯一的,如果用相同的key不斷設值得時候,保留後面的值。
2.Object get(String key) :根據key值獲取JSONObject對像中對應的value值,獲取到的值是Object類型,需要手動轉化為需要的數據類型
3.int size():獲取JSONObject對像中鍵值對的數量
4.boolean isEmpty():判斷該JSONObject對象是否為空
5.containsKey(Object key):判斷是否有需要的key值
6.boolean containsValue(Object value):判斷是否有需要的value值
7.JSONObject getJSONObject(String key):如果JSONObjct對像中的value是一個JSONObject對象,即根據key獲取對應的JSONObject對像;
8.JSONArray getJSONArray(String key) :如果JSONObject對像中的value是一個JSONObject數組,既根據key獲取對應的JSONObject數組;
9.Object remove(Object key):根據key清除某一個鍵值對。
由於JSONObject是一個map,它還具有map特有的兩個方法:
10.Set<String> keySet() :獲取JSONObject中的key,並將其放入Set集合中
11.Set<Map.Entry<String, Object>> entrySet():在循環遍歷時使用,取得是鍵和值的映射關係,Entry就是Map接口中的內部接口
與String字符串轉換:
12.toJSONString() /toString():將JSONObject對像轉換為json的字符串
常用的方法主要為以上這些,下面列出使用這些方法的example:
main(String[] args) {
新建JSONObject對像
JSONObject object1 = JSONObject();
1.在JSONObject對像中放入鍵值對
object1.put("name", "張三");
object1.put("name1", "張三1");
object1.put("name2", "張三2");
2.根據key獲取value
String name = (String) object1.get("name");
System.out.println(name);
3.獲取JSONObject中的鍵值對個數
object1.size();
System.out.println(size);
4.判斷是否為空
object1.isEmpty();
System.out.println(result);
5.是否包含對應的key值,包含返回true,不包含返回false
);
System.out.println(isContainsKeyResult);
6.是否包含對應的value值,包含返回true,不包含返回false
);
System.out.println(isContainsValueResult);
7.JSONObjct對像中的value是一個JSONObject對象,即根據key獲取對應的JSONObject對像;
JSONObject object2 = JSONObject();
將jsonobject對像作為value進行設置
object2.put("student1", object1);
JSONObject student =object2.getJSONObject("student1");
System.out.println(student);
8.如果JSONObject對像中的value是一個JSONObject數組,既根據key獲取對應的JSONObject數組;
JSONObject objectArray = JSONObject();
創建JSONArray數組
JSONArray jsonArray = JSONArray();
在JSONArray數組設值:jsonArray.add(int index, Object value);
jsonArray.add(0, "this is a jsonArray value");
jsonArray.add(1, "another jsonArray value");
objectArray.put("testArray", jsonArray);
獲取JSONObject對像中的JSONArray數組
JSONArray jsonArray2 = objectArray.getJSONArray("testArray");
System.out.println(jsonArray2);
9.remove.根據key移除JSONObject對像中的某個鍵值對
object1.remove("name");
System.out.println(object1);
10.取得JSONObject對像中key的集合
Set<String> keySet= object1.keySet();
(String key : keySet) {
System.out.print(" "+key);
}
System.out.println();
11.取得JSONObject對像中的鍵和值的映射關係
Set<Map.Entry<String, Object>> entrySet = object1.entrySet();
entry : entrySet) {
System.out.println(entry);
}
12.轉換為json字符串
String str1 = object1.toJSONString();
System.out.println(str1);
String str2 =object1.toString();
System.out.println(str2);
}
運行以上程序的結果為: