Gson
前言
之前进行网络后台开发的时候,使用Json的方式是使用JsonArray或者JsonObject进行封装的,但是最近和移动组后台合并开发,发现他们用的是Gson,赶忙学习了一下Gson-Json的一个库。
介绍
从Gson在线文档中可以看出,Gson基本上只有六个功能,其中最重要的是这两个。
返回值 | 方法 | 介绍 |
---|---|---|
<T> T | fromJson(String json, Class< T > classOfT) | This method deserializes the specified Json into an object of the specified class. |
String | toJson(Object src) | This method serializes the specified object into its equivalent Json representation. |
使用
使用-对象
从上面的介绍可以看出使用起来是很简单的,比如说我们有一个Student类需要进行序列化,这用法就是
Gson gson=new Gson();
Student s =new Student(1,"chen");//构造一个名字叫做chen的学生
String str=gson.toJson(s);//序列化
因此,str内容就是
{"id":1,"name":"chen"}
这样,你就可以看我上一篇博文JSON对他进行操作了。
那既然序列化成了字符串,怎么变回对象。
Student s1=gson.fromJson(str, Student.class);//从str变成s1对象
就是这样简单。
使用-数组
Student s1=new Student(1,"chen");
Student s2=new Student(2,"jun");
Student s3=new Student(3,"ming");
List<Student> list=new ArrayList<Student>();
list.add(s1);
list.add(s2);
list.add(s3);
str=gson.toJson(list);//数组序列化成字符串
List<Student> list2=new ArrayList<Student>();
list2=gson.fromJson(str, new TypeToken<List<Student>>() {
}.getType());//从字符串封装成数组
使用-Map
对象序列化成字符串
Map<String, Student> map = new LinkedHashMap<String, Student>();
//...map添加过程
String str = gson.toJson(map);
字符串还原成对象
Map<Point, String> newMap = gson.fromJson(str,
new TypeToken<Map<String, Student>>() {
}.getType());
使用注解
定义
注解 | 解释 |
---|---|
Expose | An annotation that indicates this member should be exposed for JSON serialization or deserialization. |
SerializedName | An annotation that indicates this member should be serialized to JSON with the provided name value as its field name. |
Since | An annotation that indicates the version number since a member or a type has been present. |
Until | An annotation that indicates the version number until a member or a type should be present. |
从上面,我根据理解和实验稍微翻译了一下,如果错了,请指正。
注解 | 解释 |
---|---|
Expose | 使用这个注解的表示要进行序列化,并且属性名首字母大写显示。 |
SerializedName | 自定义属性名。 |
Since | 指示已存在的成员或类型的版本号的批注。 |
Until | 指示该版本号的批注,该批注在成员或类型之前。 |
使用
package com.chen.model;
import java.util.Date;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
import com.google.gson.annotations.Since;
import com.google.gson.annotations.Until;
public class Student1 {
private int id;//不序列化
@Since(1.0)//版本号为(1.0)
@Expose//要导出, 并且导出时,首字母大写
private String name;
@Expose
@SerializedName("bir")//自定义名字
private Date birthDay;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getBirthDay() {
return birthDay;
}
public void setBirthDay(Date birthDay) {
this.birthDay = birthDay;
}
@Override
public String toString() {
return "Student1 [id=" + id + ", name=" + name + ", birthDay="
+ birthDay + "]";
}
public Student1(int id, String name, Date birthDay) {
super();
this.id = id;
this.name = name;
this.birthDay = birthDay;
}
public Student1() {
super();
}
}
使用Gson的方式和以前有所不同了,要使用GsonBuilder构造。
Gson gson=new GsonBuilder()
.excludeFieldsWithoutExposeAnnotation() //不导出实体中没有用@Expose注解的属性
.serializeNulls().setDateFormat("yyyy-MM-dd HH:mm:ss:SSS")//时间转化为特定格式
.setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE)//会把字段首字母大写,注:对于实体上使用了@SerializedName注解的不会生效.
.setPrettyPrinting() //对json结果格式化.
.setVersion(1.0) //有的字段不是一开始就有的,会随着版本的升级添加进来,那么在进行序列化和返序列化的时候就会根据版本号来选择是否要序列化.
//@Since(版本号)能完美地实现这个功能.还的字段可能,随着版本的升级而删除,那么
//@Until(版本号)也能实现这个功能,GsonBuilder.setVersion(double)方法需要调用.
.create();
之后的使用方式和上面一样。
其实也可以看看GsonBuilder的文档。