Gson 是 Google 提供的用來在 Java 對象和 JSON 數據之間進行映射的 Java 類庫。可以將一個 JSON 字符串轉成一個 Java 對象,或者反過來
實體類:
public class ClassRoom {
@Expose
@SerializedName("樓層")
private String floor;
@Expose
private String name;
@Expose
private Date date;
...get set ...
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
public class Student {
@Expose
private int id;
@Expose
private String name;
@Expose
private int age;
...get set ...
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
public class Teacher {
@Expose(deserialize=false)
private int id;
@Expose
private String name;
@Expose
private int age;
@Expose
private List<Student> students;
@Expose
private ClassRoom room;
...get set ...
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
測試:
import java.sql.Date;
import java.util.ArrayList;
import java.util.List;
import org.demo.model.ClassRoom;
import org.demo.model.Student;
import org.demo.model.Teacher;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
public class test {
public static void main(String[] args) {
String json = getBean();
System.out.println(json);
Gson gson = new GsonBuilder()
.excludeFieldsWithoutExposeAnnotation() //不導出實體中沒有用@Expose註解的屬性
.setDateFormat("yyyy-MM-dd HH:mm:ss")
.create();
System.out.println("--------------轉爲javaBean--------------");
Teacher teacher = gson.fromJson(json, Teacher.class);
System.out.println(teacher.toString());
ClassRoom room = teacher.getRoom();
System.out.println(room);
}
public static String getBean(){
System.out.println("--------------轉爲Json字符串--------------");
ClassRoom room = new ClassRoom();
room.setName("三年二班");
room.setFloor("5樓");
room.setDate(new Date(System.currentTimeMillis()));
Student student1 = new Student();
student1.setId(1);
student1.setName("小飛");
student1.setAge(11);
Student student2 = new Student();
student2.setId(2);
student2.setName("小天");
student2.setAge(11);
Student student3 = new Student();
student3.setId(3);
student3.setName("小恩");
student3.setAge(11);
List<Student> list = new ArrayList<Student>();
list.add(student1);
list.add(student2);
list.add(student3);
Teacher teacher = new Teacher();
teacher.setId(1);
teacher.setName("蒼老師");
teacher.setAge(32);
teacher.setStudents(list);
teacher.setRoom(room);
Gson gson = new GsonBuilder()
.setPrettyPrinting() //對json結果格式化.
.setDateFormat("yyyy-MM-dd HH:mm:ss")
.create();
//也可以用new Gson() 但有些東西可能會不符合要求
// Gson gson = new Gson();
return gson.toJson(teacher);
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
結果:
--------------轉爲Json字符串--------------
{
"id": 1,
"name": "蒼老師",
"age": 32,
"students": [
{
"id": 1,
"name": "小飛",
"age": 11
},
{
"id": 2,
"name": "小天",
"age": 11
},
{
"id": 3,
"name": "小恩",
"age": 11
}
],
"room": {
"樓層": "5樓",
"name": "三年二班",
"date": "2016-03-24 10:49:21"
}
}
--------------轉爲javaBean--------------
Teacher [id=0, name=蒼老師, age=32, students=[Student [id=1, name=小飛, age=11, teacher=null], Student [id=2, name=小天, age=11, teacher=null], Student [id=3, name=小恩, age=11, teacher=null]], room=ClassRoom [floor=5樓, name=三年二班, date=2016-03-24]]
ClassRoom [floor=5樓, name=三年二班, date=2016-03-24]
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
這裏需要注意GsonBuilder含有的方法:
- excludeFieldsWithoutExposeAnnotation() //不導出實體中沒有用@Expose註解的屬性
- enableComplexMapKeySerialization() //支持Map的key爲複雜對象的形式
- 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)方法需要調用.
Gson的註解
@Expose 屬性將不會被序列化和反序列化
@Expose(serialize=false) 屬性將不會被序列化
@Expose(deserialize=false) 屬性將不會被反序列化
@SerializedName(“xxx”) 屬性序列化後的名字
…