Gson和Fastjson的使用
轉載自:JSON技術的調研報告
一、Google的Gson包的使用簡單介紹。
Gson類:解析json的最基礎的工具類
JsonParser類:解析器來解析JSON到JsonElements的解析樹
JsonElement類:一個類代表的JSON元素
JsonObject類:JSON對象類型
JsonArray類:JsonObject數組
TypeToken類:用於創建type,比方泛型List
(1)maven依賴
com.google.code.gson
gson
2.3.1
開源地址 https://github.com/google/gson
(2)基礎轉換類
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轉換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,須要使用TypeToken
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();
二、阿裏巴巴的FastJson包的使用簡單介紹。
(1)maven依賴
com.alibaba
fastjson
1.2.7
開源地址 https://github.com/alibaba/fastjson
(2)基礎轉換類
同上
(3)bean轉換json
將對象轉換成格式化的json
JSON.toJSONString(obj,true);
將對象轉換成非格式化的json
JSON.toJSONString(obj,false);
obj設計對象
對於復雜類型的轉換,對於反復的引用在轉成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);
(4)json轉換bean
String json = "{\"id\":\"2\",\"name\":\"Json技術\"}";
Book book = JSON.parseObject(json, Book.class);
(5)json轉換復雜的bean,比方List,Map
//將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,該方法能夠使用設置的日期格式對日期進行轉換
Gson和Fastjson的使用