JAVA常用的JSON處理庫,常用處理方法
阿新 • • 發佈:2019-01-11
package cn.wangyuan.util;
import com.alibaba.fastjson.*;
/**
*
*
* @author tomato
*
************Fastjson是一個Java語言編寫的JSON處理器,由阿里巴巴公司開發。
1、遵循http://json.org標準,為其官方網站收錄的參考實現之一。
2、功能強大,支援JDK的各種型別,包括基本的JavaBean、Collection、Map、Date、Enum、泛型。
3、無依賴,不需要例外額外的jar,能夠直接跑在JDK上。
4、開源,使用Apache License 2.0協議開源。http://code.alibabatech.com/wiki/display/FastJSON/Home
5、具有超高的效能,java世界裡沒有其他的json庫能夠和fastjson可相比了。
*************鑑於fastjson優越的效能表現,我建議做如下事情;
1、替換其他所有的json庫,java世界裡沒有其他的json庫能夠和fastjson可相比了。
2、使用fastjson的序列化和反序列化替換java serialize,java serialize不單效能慢,而且體制大。
3、使用fastjson替換hessian,json協議不必hessian體積大,而且fastjson效能優越,數倍於hessian
4、把fastjson用於memached快取物件資料。
*
*/
public class FastJsonUtil {
public static void main(String[] args) {
//fastjson pojo to bean
MyBean myBean=new MyBean();
String jsonString=JSON.toJSONString(myBean);
System.out.println(jsonString);
// fastjson json to string,parse json
String jsonStr = "{\"JACKIE_ZHANG\":\"張學友\",\"ANDY_LAU\":\"劉德華\",\"LIMING\":\"黎明\",\"Aaron_Kwok\":\"郭富城\"}" ;
JSONObject jsonObject = JSONObject.parseObject(jsonStr) ;
//輸出結果
System.out.println(jsonObject);
System.out.println("li->"+jsonObject.get("LIMING"));
//輸出
for(java.util.Map.Entry<String,Object> entry : jsonObject.entrySet()){
System.out.println(entry.getKey()+"-"+entry.getValue()+"\t");
}
}
}