Gson 是 Google 提供的用来在 Java 对象和 JSON 数据之间进行映射的 Java 类库。可以将一个 JSON 字符串转成一个 Java 对象,或者反过来。
先自定义一个Class类
public class Student {
public int id;
public String nickName;
public int age;
public Date birthDay;
public ArrayList<String> books;
public HashMap<String, String> booksMap;
public Student(int id, String nickName, int age) {
super();
this.id = id;
this.nickName = nickName;
this.age = age;
}
@Override
public String toString() {
return "[id = "+id + ",nickName=" + nickName + ",age=" + age+"]";
}
}
案例一:仅包含基本数据类型的数据结构
Gson gson = new Gson();
Student student =new Student(1, "李三", 20);
Log.e("GsonActivity", gson.toJson(student)); </span>
输出结果:
{"age":20,"id":1,"nickName":"李三"}
案例二:除了基本数据类型还包含了List集合
Gson gson =new Gson();
Student student =new Student(1, "李三", 20);
ArrayList<String> booksList =new ArrayList<String>();
booksList.add("java");
booksList.add("ios");
booksList.add("c++");
student.books = booksList;
Log.e("GsonActivity", gson.toJson(student)); </span>
输出结果:
{"age":20,"books":["java","ios","c++"],"id":1,"nickName":"李三"}
案例三:除了基本数据类型还包含了List和Map集合
Gson gson =new Gson();
Student student =new Student(1, "李三", 20);
ArrayList<String> booksList =new ArrayList<String>();
booksList.add("java");
booksList.add("ios");
booksList.add("c++");
student.books = booksList;
HashMap<String, String> booksMap =new HashMap<String, String>();
booksMap.put("1", "数学");
booksMap.put("2", "语文");
booksMap.put("3", "英语");
booksMap.put("4", "物理");
booksMap.put("5", "化学");
booksMap.put("6", "生物");
student.booksMap = booksMap;
Log.e("GsonActivity", gson.toJson(student));</span>
输出结果:
{"age":20,"books":["java","ios","c++"],"booksMap":{"5":"化学","4":"物理","1":"数学","3":"英语","6":"生物","2":"语文"},"id":1,"nickName":"李三"}
案例四:将json字符串转换为Student对象
Gson gson =new Gson();
Student student =new Student(1, "李三", 20);
ArrayList<String> booksList =new ArrayList<String>();
booksList.add("java");
booksList.add("ios");
booksList.add("c++");
student.books = booksList;
HashMap<String, String> booksMap =new HashMap<String, String>();
booksMap.put("1", "数学");
booksMap.put("2", "语文");
booksMap.put("3", "英语");
booksMap.put("4", "物理");
booksMap.put("5", "化学");
booksMap.put("6", "生物");
student.booksMap = booksMap;
String jsonString =gson.toJson(student);
Student student2 =gson.fromJson(jsonString, Student.class);
Log.e("GsonActivity", student2.nickName + ","+ student2.age + "," + student2.books + "," + student2.booksMap); </span>
输出结果:
李三,20,[java, ios, c++],{4=物理, 5=化学, 1=数学, 3=英语, 6=生物, 2=语文}
案例五:泛型的使用
Gson gson =new Gson();
List<Student> students =new ArrayList<Student>();
Student student1 =new Student(1, "李三", 20);
Student student2 =new Student(2, "李四", 30);
Student student3 =new Student(3, "王泽瑞", 40);
students.add(student1);
students.add(student2);
students.add(student3);
String listString = gson.toJson(students);
TypeToken<List<Student>> token =new com.google.gson.reflect.TypeToken<List<Student>>(){};
List<Student> list =gson.fromJson(listString, token.getType());
for (Student stu : list) {
<span style="font-family:微软雅黑,Verdana,sans-serif,宋体;"> </span>Log.e("GsonActivity", stu.toString());
} </span>
输出结果:
[id = 1,nickName=李三,age=20]
[id = 2,nickName=李四,age=30]
[id = 3,nickName=王泽瑞,age=40]