FastJson中JSONObject用法及常用方法總結


本文為博主原創,未經允許不得轉載:

  最近一直有用到解析各種數據,主要是用FastJson進行數據解析,其中一個重要的類為JSONObject,今天有時間,所以進行總結一下:

JSONobject是FastJson提供的對象,在api中是用一個私有的常量map進行封裝的,實際就是一個map,只不過FastJson對其進行了封裝,

添加了很多方便快捷的屬性方法。

private 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:

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);
    }

 運行以上程序的結果為:

最近感悟(不喜勿噴):

越長大越理解得別人的不容易,也發現自己更多的無奈和不容易,有很多想去照顧的人,想去做的事,卻發現自己的能力只能承擔起一丁點。想家了


免責聲明!

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



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