Fast-Json 詳解 — JSONObject與JSONArray的用法

FastJson:fastjson是阿里巴巴的開源JSON解析庫,它可以解析JSON格式的字符串,支持將Java Bean序列化爲JSON字符串,也可以從JSON字符串反序列化到JavaBean,本篇章只講述Json與對象之間的轉換。至於fastJson在Spring或者jarx等框架中的使用本篇不作講述。在使用Fast-Josn時,我們只需要引入阿里的fast-json庫即可

<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>1.2.62</version>
</dependency>

Fastjson主要的API是 com.alibaba.fastjson.JSON,com.alibaba.fastjson.JSONObject和com.alibaba.fastjson.JSONArray。JSONObject表示JSON對象,JSONArray表示JSON數組,下面一個入門案例展示如何創建JSON對象和JSON數組。

		//創建JSON對象
		JSONObject jsonObject = new JSONObject();
		//put方法往json存儲字段,key爲String,Value爲Object
		jsonObject.put("id", 1);
		jsonObject.put("name", "pharos");
		Map<String,Object> hashMap = new HashMap<String,Object>();
		hashMap.put("age", 25);
		hashMap.put("isWorker", true);
		//存儲hashMap,其實將Map轉換爲JSON格式。
		jsonObject.putAll(hashMap);
		//fluentPut流式編程fluentPutAll也是如此,不再示例
		jsonObject.fluentPut("email", "[email protected]").put("qq", "813844896");
		//創建JSONArray對象
		JSONArray jsonArray = new JSONArray();
		//JSONArray中有add方法,往json數組中添加一個元素,
		//與add方法類似,還有fluentAdd方法,流式編程
		jsonArray.add("music");
		jsonArray.add(1,"running");
		//直接添加集合,添加多個元素
		//與add方法類似,還有fluentAddAll方法,流式編程
		jsonArray.addAll(new ArrayList<Object>());
		jsonArray.addAll(4, new ArrayList<Object>());
		jsonObject.put("hobby", jsonArray);

從上面的例子已經API可以看到JSONObject和JSONArray存儲的數組均爲Object,再從其取數據時,他們提供了get方法返回Object類型,如果知道存儲的是具體的類型,還可以通過getXxx獲取具體類型的數據。如下爲其部分API的使用:

                //獲取布爾型
		Boolean isWorker = jsonObject.getBoolean("isWorker");
		boolean isWorkerValue = jsonObject.getBooleanValue("isWorker");
		//獲取整型
		Integer id = jsonObject.getInteger("id");
		int idValue = jsonObject.getIntValue("id");
		//獲取字符串類型
		String name = jsonObject.getString("name");
		//獲取Object類型
		Object qq = jsonObject.get("qq");
                
                //JSONArray是根據索引獲取不同類型的數據
                jsonArray.get(0);
		jsonArray.getInteger(1);
		jsonArray.getBooleanValue(2);

上面的案例是JSONObject獲取整型、字符串、布爾型的數值,JSONArray也有如此類似方法,不再舉例子,除此之外,還有getBigDecimal,getBigInteger,getByte,getDouble,getJSONArray和getObject等方法,getObject方法可以將JSON轉換爲對應的實體類。JSONObject,JSONArray有如此多的get方法,我們就需要關注如果JSONuObject的key不存在或者JSONArray的索引不存在時是怎麼處理的,默認值是多少。如下列表爲key不存在時的默認值。

fast-json JSONObject value默認值
method value
get(Object key) null
getBooleanValue(String key) false
getBoolean(String key) null
getIntValue(String key) 0
getByteValue(String key) 0
getShortValue(String key) 0
getByte(String key) null
getBigInteger(String key) null
getInteger(String key) null
getDoubleValue(String key) 0.0
getFloatValue(String key) 0.0

上面列舉了一部分JSONObject的默認值,總結起來就是非基本類型的數據(包括包裝類)默認值爲null,基本類型的數據布爾型默認爲false,整型默認爲0,浮點型默認爲0.0。如果獲取時,類型轉換失敗,則拋出異常。比如:

Exception in thread "main" java.lang.NumberFormatException: For input string: "pharos"
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
    at java.lang.Integer.parseInt(Integer.java:580)
    at java.lang.Integer.parseInt(Integer.java:615)
    at com.alibaba.fastjson.util.TypeUtils.castToInt(TypeUtils.java:817)
    at com.alibaba.fastjson.JSONObject.getIntValue(JSONObject.java:243)
    at cn.org.microservice.fastjson.starter.FastJsonStarter.main(FastJsonStarter.java:59)

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