FastJson中JSONObject用法及常用方法總結
SON協議使用方便,越來越流行,JSON的處理器有很多,這裡我介紹一下FastJson,FastJson是阿里的開源框架,被不少企業使用,是一個極其優秀的Json框架,Github地址: FastJson
FastJson對於json格式字串的解析主要用到了下面三個類:
1.JSON:fastJson的解析器,用於JSON格式字串與JSON物件及javaBean之間的轉換
2.JSONObject:fastJson提供的json物件
3.JSONArray:fastJson提供json陣列物件
連結:JSON格式字串與JSON物件之間的轉換
主要是用FastJson進行資料解析,其中一個重要的類為JSONObject;
先來看下它有哪些常用方法,以及有什麼作用:
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的字串
常用的方法有以下:
public static void main(String[] args) {
//新建JSONObject物件
JSONObject object1 = new 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中的鍵值對個數
int size = object1.size();
System.out.println(size);
//4.判斷是否為空
boolean result = object1.isEmpty();
System.out.println(result);
//5.是否包含對應的key值,包含返回true,不包含返回false
boolean isContainsKeyResult = object1.containsKey("name");
System.out.println(isContainsKeyResult);
//6.是否包含對應的value值,包含返回true,不包含返回false
boolean isContainsValueResult = object1.containsValue("王五");
System.out.println(isContainsValueResult);
//7.JSONObjct物件中的value是一個JSONObject物件,即根據key獲取對應的JSONObject物件;
JSONObject object2 = new JSONObject();
//將jsonobject物件作為value進行設定
object2.put("student1", object1);
JSONObject student =object2.getJSONObject("student1");
System.out.println(student);
//8.如果JSONObject物件中的value是一個JSONObject陣列,既根據key獲取對應的JSONObject陣列;
JSONObject objectArray = new JSONObject();
//建立JSONArray陣列
JSONArray jsonArray = new 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();
for (String key : keySet) {
System.out.print(" "+key);
}
System.out.println();
//11.取得JSONObject物件中的鍵和值的對映關係
Set<Map.Entry<String, Object>> entrySet = object1.entrySet();
for (Entry<String, Object> entry : entrySet) {
System.out.println(entry);
}
//12.轉換為json字串
String str1 = object1.toJSONString();
System.out.println(str1);
String str2 =object1.toString();
System.out.println(str2);
}