JSON技術(shù)的調(diào)研報(bào)告
一 哈踱、各個(gè)JSON技術(shù)的簡(jiǎn)介和優(yōu)劣
1.json-lib
json-lib最開始的也是應(yīng)用最廣泛的json解析工具,json-lib 不好的地方確實(shí)是依賴于很多第三方包梨熙,
包括commons-beanutils.jar开镣,commons-collections-3.2.jar,commons-lang-2.6.jar咽扇,commons-logging-1.1.1.jar邪财,ezmorph-1.0.6.jar,
對(duì)于復(fù)雜類型的轉(zhuǎn)換质欲,json-lib對(duì)于json轉(zhuǎn)換成bean還有缺陷树埠,比如一個(gè)類里面會(huì)出現(xiàn)另一個(gè)類的list或者map集合,json-lib從json到bean的轉(zhuǎn)換就會(huì)出現(xiàn)問題把敞。
json-lib在功能和性能上面都不能滿足現(xiàn)在互聯(lián)網(wǎng)化的需求弥奸。
2.開源的Jackson
相比json-lib框架,Jackson所依賴的jar包較少奋早,簡(jiǎn)單易用并且性能也要相對(duì)高些盛霎。
而且Jackson社區(qū)相對(duì)比較活躍赠橙,更新速度也比較快。
Jackson對(duì)于復(fù)雜類型的json轉(zhuǎn)換bean會(huì)出現(xiàn)問題愤炸,一些集合Map期揪,List的轉(zhuǎn)換出現(xiàn)問題。
Jackson對(duì)于復(fù)雜類型的bean轉(zhuǎn)換Json规个,轉(zhuǎn)換的json格式不是標(biāo)準(zhǔn)的Json格式
3.Google的Gson
Gson是目前功能最全的Json解析神器凤薛,Gson當(dāng)初是為因應(yīng)Google公司內(nèi)部需求而由Google自行研發(fā)而來,
但自從在2008年五月公開發(fā)布第一版后已被許多公司或用戶應(yīng)用诞仓。
Gson的應(yīng)用主要為toJson與fromJson兩個(gè)轉(zhuǎn)換函數(shù)缤苫,無依賴,不需要例外額外的jar墅拭,能夠直接跑在JDK上活玲。
而在使用這種對(duì)象轉(zhuǎn)換之前需先創(chuàng)建好對(duì)象的類型以及其成員才能成功的將JSON字符串成功轉(zhuǎn)換成相對(duì)應(yīng)的對(duì)象。
類里面只要有g(shù)et和set方法谍婉,Gson完全可以將復(fù)雜類型的json到bean或bean到j(luò)son的轉(zhuǎn)換舒憾,是JSON解析的神器。
Gson在功能上面無可挑剔穗熬,但是性能上面比FastJson有所差距镀迂。
4.阿里巴巴的FastJson
Fastjson是一個(gè)Java語言編寫的高性能的JSON處理器,由阿里巴巴公司開發(fā)。
無依賴唤蔗,不需要例外額外的jar探遵,能夠直接跑在JDK上。
FastJson在復(fù)雜類型的Bean轉(zhuǎn)換Json上會(huì)出現(xiàn)一些問題妓柜,可能會(huì)出現(xiàn)引用的類型别凤,導(dǎo)致Json轉(zhuǎn)換出錯(cuò),需要制定引用领虹。
FastJson采用獨(dú)創(chuàng)的算法,將parse的速度提升到極致求豫,超過所有json庫塌衰。
綜上4種Json技術(shù)的比較,在項(xiàng)目選型的時(shí)候可以使用Google的Gson和阿里巴巴的FastJson兩種并行使用蝠嘉,
如果只是功能要求最疆,沒有性能要求,可以使用google的Gson蚤告,
如果有性能上面的要求可以使用Gson將bean轉(zhuǎn)換json確保數(shù)據(jù)的正確努酸,使用FastJson將Json轉(zhuǎn)換Bean
二、Google的Gson包的使用簡(jiǎn)介杜恰。
Gson類:解析json的最基礎(chǔ)的工具類
JsonParser類:解析器來解析JSON到JsonElements的解析樹
JsonElement類:一個(gè)類代表的JSON元素
JsonObject類:JSON對(duì)象類型
JsonArray類:JsonObject數(shù)組
TypeToken類:用于創(chuàng)建type获诈,比如泛型List
(1)maven依賴
com.google.code.gson
gson
2.2.4
(2)基礎(chǔ)轉(zhuǎn)換類
public class Book{
private String id;
private String name;
public Book() {
super();
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
public class Student{
private String name;
private int age;
private String sex;
private String describe;
private Set books;
public Student() {
super();
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public Set getBooks() {
return books;
}
public void setBooks(Set books) {
this.books = books;
}
public String getDescribe() {
return describe;
}
public void setDescribe(String describe) {
this.describe = describe;
}
}
(3)bean轉(zhuǎn)換json
Gson gson = new Gson();
String json = gson.toJson(obj);
obj是對(duì)象
(4)json轉(zhuǎn)換bean
Gson gson = new Gson();
String json = "{\"id\":\"2\",\"name\":\"Json技術(shù)\"}";
Book book = gson.fromJson(json, Book.class);
(5)json轉(zhuǎn)換復(fù)雜的bean仍源,比如List,Set
將json轉(zhuǎn)換成復(fù)雜類型的bean,需要使用TypeToken
Gson gson = new Gson();
String json = "[{\"id\":\"1\",\"name\":\"Json技術(shù)\"},{\"id\":\"2\",\"name\":\"java技術(shù)\"}]";
//將json轉(zhuǎn)換成List
List list = gson.fromJson(json,new TypeToken() {}.getType());
//將json轉(zhuǎn)換成Set
Set set = gson.fromJson(json,new TypeToken() {}.getType());
(6)通過json對(duì)象直接操作json以及一些json的工具
a)格式化Json
String json = "[{\"id\":\"1\",\"name\":\"Json技術(shù)\"},{\"id\":\"2\",\"name\":\"java技術(shù)\"}]";
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技術(shù)\"},{\"id\":\"2\",\"name\":\"java技術(shù)\"}]";
boolean jsonFlag;
try {
new JsonParser().parse(str).getAsJsonObject();
jsonFlag = true;
} catch (Exception e) {
jsonFlag = false;
}
c)從json串中獲取屬性
String json = "{\"id\":\"1\",\"name\":\"Json技術(shù)\"}";
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中的某個(gè)屬性
String json = "{\"id\":\"1\",\"name\":\"Json技術(shù)\"}";
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技術(shù)\"}";
String propertyName = 'desc';
Object propertyValue = "json各種技術(shù)的調(diào)研";
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技術(shù)\"}";
String propertyName = 'name';
Object propertyValue = "json各種技術(shù)的調(diào)研";
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技術(shù)\"}";
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對(duì)象進(jìn)行json的處理舔涎,如果出現(xiàn)日期Date類的對(duì)象笼踩,就會(huì)按照設(shè)置的格式進(jìn)行處理
i)json中對(duì)于Html的轉(zhuǎn)義
Gson gson = new Gson();
這種對(duì)象默認(rèn)對(duì)Html進(jìn)行轉(zhuǎn)義,如果不想轉(zhuǎn)義使用下面的方法
GsonBuilder builder = new GsonBuilder();
builder.disableHtmlEscaping();
Gson gson = builder.create();
三亡嫌、阿里巴巴的FastJson包的使用簡(jiǎn)介嚎于。
(1)maven依賴
com.alibaba
fastjson
1.1.22
(2)基礎(chǔ)轉(zhuǎn)換類
同上
(3)bean轉(zhuǎn)換json
將對(duì)象轉(zhuǎn)換成格式化的json
JSON.toJSONString(obj,true);
將對(duì)象轉(zhuǎn)換成非格式化的json
JSON.toJSONString(obj,false);
obj設(shè)計(jì)對(duì)象
對(duì)于復(fù)雜類型的轉(zhuǎn)換,對(duì)于重復(fù)的引用在轉(zhuǎn)成json串后在json串中出現(xiàn)引用的字符,比如 $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);
(4)json轉(zhuǎn)換bean
String json = "{\"id\":\"2\",\"name\":\"Json技術(shù)\"}";
Book book = JSON.parseObject(json, Book.class);
(5)json轉(zhuǎn)換復(fù)雜的bean,比如List挟冠,Map
String json = "[{\"id\":\"1\",\"name\":\"Json技術(shù)\"},{\"id\":\"2\",\"name\":\"java技術(shù)\"}]";
//將json轉(zhuǎn)換成List
List list = JSON.parseObject(json,new TypeReference(){});
//將json轉(zhuǎn)換成Set
Set set = JSON.parseObject(json,new TypeReference(){});
(6)通過json對(duì)象直接操作json
a)從json串中獲取屬性
String propertyName = 'id';
String propertyValue = "";
String json = "{\"id\":\"1\",\"name\":\"Json技術(shù)\"}";
JSONObject obj = JSON.parseObject(json);
propertyValue = obj.get(propertyName));
b)除去json中的某個(gè)屬性
String propertyName = 'id';
String propertyValue = "";
String json = "{\"id\":\"1\",\"name\":\"Json技術(shù)\"}";
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技術(shù)\"}";
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技術(shù)\"}";
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技術(shù)\"}";
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,該方法可以使用設(shè)置的日期格式對(duì)日期進(jìn)行轉(zhuǎn)換
三于购、json-lib包的使用簡(jiǎn)介。
(1)maven依賴
net.sf.json-lib
json-lib
jdk15
2.2.2
commons-beanutils
commons-beanutils
1.8.3
commons-collections
commons-collections
3.2
commons-lang
commons-lang
2.6
commons-logging?
commons-logging?
1.1.1?
net.sf.ezmorph
ezmorph
1.0.6
(2)基礎(chǔ)轉(zhuǎn)換類
同上
(3)bean轉(zhuǎn)換json
a)將類轉(zhuǎn)換成Json,obj是普通的對(duì)象知染,不是List肋僧,Map的對(duì)象
String json = JSONObject.fromObject(obj).toString();
b)將List,Map轉(zhuǎn)換成Json
String json = JSONArray.fromObject(list).toString();
String json = JSONArray.fromObject(map).toString();
(4)json轉(zhuǎn)換bean
String json = "{\"id\":\"1\",\"name\":\"Json技術(shù)\"}";
JSONObject jsonObj = JSONObject.fromObject(json);
Book book = (Book)JSONObject.toBean(jsonObj,Book.class);
(5)json轉(zhuǎn)換List,對(duì)于復(fù)雜類型的轉(zhuǎn)換會(huì)出現(xiàn)問題
String json = "[{\"id\":\"1\",\"name\":\"Json技術(shù)\"},{\"id\":\"2\",\"name\":\"Java技術(shù)\"}]";
JSONArray jsonArray = JSONArray.fromObject(json);
JSONObject jsonObject;
T bean;
int size = jsonArray.size();
List list = new ArrayList(size);
for (int i = 0; i < size; i++) {
jsonObject = jsonArray.getJSONObject(i);
bean = (T) JSONObject.toBean(jsonObject, beanClass);
list.add(bean);
}
(6)json轉(zhuǎn)換Map
String jsonString = "{\"id\":\"1\",\"name\":\"Json技術(shù)\"}";
JSONObject jsonObject = JSONObject.fromObject(jsonString);
Iterator keyIter = jsonObject.keys();
String key;
Object value;
Map valueMap = new HashMap();
while (keyIter.hasNext()) {
key = (String) keyIter.next();
value = jsonObject.get(key).toString();
valueMap.put(key, value);
}
(7)json對(duì)于日期的操作比較復(fù)雜持舆,需要使用JsonConfig,比Gson和FastJson要麻煩多了
創(chuàng)建轉(zhuǎn)換的接口實(shí)現(xiàn)類色瘩,轉(zhuǎn)換成指定格式的日期
class DateJsonValueProcessor implements JsonValueProcessor{
public static final String DEFAULT_DATE_PATTERN = "yyyy-MM-dd HH:mm:ss.SSS";?
private DateFormat dateFormat;?
public DateJsonValueProcessor(String datePattern) {?
try {?
dateFormat = new SimpleDateFormat(datePattern);?
} catch (Exception ex) {?
dateFormat = new SimpleDateFormat(DEFAULT_DATE_PATTERN);?
}?
}?
public Object processArrayValue(Object value, JsonConfig jsonConfig) {?
return process(value);?
}?
public Object processObjectValue(String key, Object value,?
JsonConfig jsonConfig) {?
return process(value);?
}?
private Object process(Object value) {?
Map birthDays = new HashMap();
birthDays.put("WolfKing",new Date());
JSONObject jsonObject = JSONObject.fromObject(birthDays, jsonConfig);
String json = jsonObject.toString();
System.out.println(json);
}
}
(8)JsonObject 對(duì)于json的操作和處理
a)從json串中獲取屬性
String jsonString = "{\"id\":\"1\",\"name\":\"Json技術(shù)\"}";
Object key = "name";
Object value = null;
JSONObject jsonObject = JSONObject.fromObject(jsonString);
value = jsonObject.get(key);
jsonString = jsonObject.toString();
b)除去json中的某個(gè)屬性
String jsonString = "{\"id\":\"1\",\"name\":\"Json技術(shù)\"}";
Object key = "name";
Object value = null;
JSONObject jsonObject = JSONObject.fromObject(jsonString);
value = jsonObject.remove(key);
jsonString = jsonObject.toString();
c)向json中添加和修改屬性,有則修改逸寓,無則添加
String jsonString = "{\"id\":\"1\",\"name\":\"Json技術(shù)\"}";
Object key = "desc";
Object value = "json的好東西";
JSONObject jsonObject = JSONObject.fromObject(jsonString);
jsonObject.put(key,value);
jsonString = jsonObject.toString();
d)判斷json中是否有屬性
String jsonString = "{\"id\":\"1\",\"name\":\"Json技術(shù)\"}";
boolean containFlag = false;
Object key = "desc";
JSONObject jsonObject = JSONObject.fromObject(jsonString);
containFlag = jsonObject.containsKey(key);