FastJson中JSONObject用法及常用方法

FastJson對於json格式字符串的解析主要用到了下面三個類:
1.JSON:fastJson的解析器,用於JSON格式字符串與JSON對象及javaBean之間的轉換
2.JSONObject:fastJson提供的json對象
3.JSONArray:fastJson提供json數組對象
依賴包

<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 keySet() :獲取JSONObject中的key,並將其放入Set集合中

11.Set<Map.Entry<String, Object>> entrySet():在循環遍歷時使用,取得是鍵和值的映射關係,Entry就是Map接口中的內部接口

與String字符串轉換:

12.toJSONString() /toString():將JSONObject對象轉換爲json的字符串


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.util.Map;
import java.util.Set;

public class Test {

    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 (Map.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);
    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章