Json解析工具对比
生活随笔
收集整理的這篇文章主要介紹了
Json解析工具对比
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
一 、各個JSON技術的簡介和優劣
1.json-lib
json-lib最開始的也是應用最廣泛的json解析工具,json-lib 不好的地方確實是依賴于很多第三方包,包括commons-beanutils.jar,commons-collections-3.2.jar,commons-lang-2.6.jar,commons-logging-1.1.1.jar,ezmorph-1.0.6.jar,
對于復雜類型的轉換,json-lib對于json轉換成bean還有缺陷,比如一個類里面會出現另一個類的list或者map集合,json-lib從json到bean的轉換就會出現問題。
json-lib在功能和性能上面都不能滿足現在互聯網化的需求。
2.開源的Jackson
相比json-lib框架,Jackson所依賴的jar包較少,簡單易用并且性能也要相對高些。而且Jackson社區相對比較活躍,更新速度也比較快。
Jackson對于復雜類型的json轉換bean會出現問題,一些集合Map,List的轉換出現問題。
Jackson對于復雜類型的bean轉換Json,轉換的json格式不是標準的Json格式
3.Google的Gson
Gson是目前功能最全的Json解析神器,Gson當初是為因應Google公司內部需求而由Google自行研發而來,但自從在2008年五月公開發布第一版后已被許多公司或用戶應用。
Gson的應用主要為toJson與fromJson兩個轉換函數,無依賴,不需要例外額外的jar,能夠直接跑在JDK上。
而在使用這種對象轉換之前需先創建好對象的類型以及其成員才能成功的將JSON字符串成功轉換成相對應的對象。
類里面只要有get和set方法,Gson完全可以將復雜類型的json到bean或bean到json的轉換,是JSON解析的神器。
Gson在功能上面無可挑剔,但是性能上面比FastJson有所差距。
4.阿里巴巴的FastJson
Fastjson是一個Java語言編寫的高性能的JSON處理器,由阿里巴巴公司開發。無依賴,不需要例外額外的jar,能夠直接跑在JDK上。
FastJson在復雜類型的Bean轉換Json上會出現一些問題,可能會出現引用的類型,導致Json轉換出錯,需要制定引用。
FastJson采用獨創的算法,將parse的速度提升到極致,超過所有json庫。
綜上4種Json技術的比較,在項目選型的時候可以使用Google的Gson和阿里巴巴的FastJson兩種并行使用,
如果只是功能要求,沒有性能要求,可以使用google的Gson,
如果有性能上面的要求可以使用Gson將bean轉換json確保數據的正確,使用FastJson將Json轉換Bean
二。使用簡介
1、Google的Gson包的使用簡介。
Gson類:解析json的最基礎的工具類JsonParser類:解析器來解析JSON到JsonElements的解析樹
JsonElement類:一個類代表的JSON元素
JsonObject類:JSON對象類型
JsonArray類:JsonObject數組
TypeToken類:用于創建type,比如泛型List<?>
(1)maven依賴
com.google.code.gson
gson
2.2.4
(2)基礎轉換類
[java]?view plaincopy(3)bean轉換json
Gson gson = new Gson();String json = gson.toJson(obj);
obj是對象
(4)json轉換bean
Gson gson = new Gson();String json = "{\"id\":\"2\",\"name\":\"Json技術\"}";
Book book = gson.fromJson(json, Book.class);
(5)json轉換復雜的bean,比如List,Set
將json轉換成復雜類型的bean,需要使用TypeTokenGson gson = new Gson();
String json = "[{\"id\":\"1\",\"name\":\"Json技術\"},{\"id\":\"2\",\"name\":\"java技術\"}]";
//將json轉換成List
List list = gson.fromJson(json,new TypeToken<LIST>() {}.getType());
//將json轉換成Set
Set set = gson.fromJson(json,new TypeToken<SET>() {}.getType());
(6)通過json對象直接操作json以及一些json的工具
a)格式化Json
String json = "[{\"id\":\"1\",\"name\":\"Json技術\"},{\"id\":\"2\",\"name\":\"java技術\"}]";Gson gson = new GsonBuilder().setPrettyPrinting().create();
JsonParser jp = new JsonParser();
JsonElement je = jp.parse(json);
json = gson.toJson(je);
b)判斷字符串是否是json,通過捕捉的異常來判斷是否是json
String json = "[{\"id\":\"1\",\"name\":\"Json技術\"},{\"id\":\"2\",\"name\":\"java技術\"}]";boolean jsonFlag;
try {
new JsonParser().parse(str).getAsJsonObject();
jsonFlag = true;
} catch (Exception e) {
jsonFlag = false;
}
c)從json串中獲取屬性
String json = "{\"id\":\"1\",\"name\":\"Json技術\"}";String propertyName = 'id';
String propertyValue = "";
try {
JsonParser jsonParser = new JsonParser();
JsonElement element = jsonParser.parse(json);
JsonObject jsonObj = element.getAsJsonObject();
propertyValue = jsonObj.get(propertyName).toString();
} catch (Exception e) {
propertyValue = null;
}
d)除去json中的某個屬性
String json = "{\"id\":\"1\",\"name\":\"Json技術\"}";String propertyName = 'id';
JsonParser jsonParser = new JsonParser();
JsonElement element = jsonParser.parse(json);
JsonObject jsonObj = element.getAsJsonObject();
jsonObj.remove(propertyName);
json = jsonObj.toString();
e)向json中添加屬性
String json = "{\"id\":\"1\",\"name\":\"Json技術\"}";String propertyName = 'desc';
Object propertyValue = "json各種技術的調研";
JsonParser jsonParser = new JsonParser();
JsonElement element = jsonParser.parse(json);
JsonObject jsonObj = element.getAsJsonObject();
jsonObj.addProperty(propertyName, new Gson().toJson(propertyValue));
json = jsonObj.toString();
f)修改json中的屬性
String json = "{\"id\":\"1\",\"name\":\"Json技術\"}";String propertyName = 'name';
Object propertyValue = "json各種技術的調研";
JsonParser jsonParser = new JsonParser();
JsonElement element = jsonParser.parse(json);
JsonObject jsonObj = element.getAsJsonObject();
jsonObj.remove(propertyName);
jsonObj.addProperty(propertyName, new Gson().toJson(propertyValue));
json = jsonObj.toString();
g)判斷json中是否有屬性
String json = "{\"id\":\"1\",\"name\":\"Json技術\"}";String propertyName = 'name';
boolean isContains = false ;
JsonParser jsonParser = new JsonParser();
JsonElement element = jsonParser.parse(json);
JsonObject jsonObj = element.getAsJsonObject();
isContains = jsonObj.has(propertyName);
h)json中日期格式的處理
GsonBuilder builder = new GsonBuilder();builder.setDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
Gson gson = builder.create();
然后使用gson對象進行json的處理,如果出現日期Date類的對象,就會按照設置的格式進行處理
i)json中對于Html的轉義
Gson gson = new Gson();
這種對象默認對Html進行轉義,如果不想轉義使用下面的方法
GsonBuilder builder = new GsonBuilder();
builder.disableHtmlEscaping();
Gson gson = builder.create();
2、阿里巴巴的FastJson包的使用簡介。
(1)maven依賴
com.alibaba
fastjson
1.1.22
(2)基礎轉換類
同上(3)bean轉換json
1.將對象轉換成格式化的jsonJSON.toJSONString(obj,true);
2.將對象轉換成非格式化的json
JSON.toJSONString(obj,false);3.對于復雜類型的轉換,對于重復的引用在轉成json串后在json串中出現引用的字符,比如 $ref":"$[0].books[1]Student stu = new Student();
Set books= new HashSet();Book book = new Book();
books.add(book);
stu.setBooks(books);
List list = new ArrayList();
for(int i=0;i<5;i++)
list.add(stu);
String json = JSON.toJSONString(list,true);
一般的列表/數組轉json
List<FastEntity> fastList = new ArrayList<>(); for (int i = 0; i < 3; i++) { FastEntity fast = new FastEntity(i, "第" + i); fastList.add(fast); Log.e("resultttt", "bean-->json結果" + JSON.toJSONString(fast)); } Log.e("resultttt", "list-->json結果" + JSON.toJSONString(fastList)); Log.e("resultttt", "arrsy-->json結果" + JSON.toJSONString(fastList.toArray())); Log.e("resultttt", "list-->json結果" + JSON.toJSONString(Arrays.asList(fastList.toArray())));(4)json轉換bean
String json = "{\"id\":\"2\",\"name\":\"Json技術\"}";Book book = JSON.parseObject(json, Book.class);
(5)json轉換復雜的bean,比如List,Map
String json = "[{\"id\":\"1\",\"name\":\"Json技術\"},{\"id\":\"2\",\"name\":\"java技術\"}]";//將json轉換成List
List list = JSON.parseObject(json,new TypeReference<ARRAYLIST>(){});
//將json轉換成Set
Set set = JSON.parseObject(json,new TypeReference<HASHSET>(){});
(6)通過json對象直接操作json
a)從json串中獲取屬性
String propertyName = 'id';String propertyValue = "";
String json = "{\"id\":\"1\",\"name\":\"Json技術\"}";
JSONObject obj = JSON.parseObject(json);
propertyValue = obj.get(propertyName));
b)除去json中的某個屬性
String propertyName = 'id';String propertyValue = "";
String json = "{\"id\":\"1\",\"name\":\"Json技術\"}";
JSONObject obj = JSON.parseObject(json);
Set set = obj.keySet();
propertyValue = set.remove(propertyName);
json = obj.toString();
c)向json中添加屬性
String propertyName = 'desc';Object propertyValue = "json的玩意兒";
String json = "{\"id\":\"1\",\"name\":\"Json技術\"}";
JSONObject obj = JSON.parseObject(json);
obj.put(propertyName, JSON.toJSONString(propertyValue));
json = obj.toString();
d)修改json中的屬性
String propertyName = 'name';Object propertyValue = "json的玩意兒";
String json = "{\"id\":\"1\",\"name\":\"Json技術\"}";
JSONObject obj = JSON.parseObject(json);
Set set = obj.keySet();
if(set.contains(propertyName))
obj.put(propertyName, JSON.toJSONString(propertyValue));
json = obj.toString();
e)判斷json中是否有屬性
String propertyName = 'name';boolean isContain = false;
String json = "{\"id\":\"1\",\"name\":\"Json技術\"}";
JSONObject obj = JSON.parseObject(json);
Set set = obj.keySet();
isContain = set.contains(propertyName);
f)json中日期格式的處理
Object obj = new Date();String json = JSON.toJSONStringWithDateFormat(obj, "yyyy-MM-dd HH:mm:ss.SSS");
使用JSON.toJSONStringWithDateFormat,該方法可以使用設置的日期格式對日期進行轉換
總結
以上是生活随笔為你收集整理的Json解析工具对比的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: visualstudio调试html,V
- 下一篇: SAP RFC 获取BDC 消息文本的实