1.1 JSON 全称: JavaScript Object Notation
1.2 Android studio 中创建JSON文件: File -> New -> Scratch File -> JSON
1.3 transient 关键字: 不参与序列化
1.4 @SerializedName 序列化,反序化时,自定义标记的名称
- dependencies {
- implementation 'com.google.code.gson:gson:2.9.1'
- }
- public class Student {
- @SerializedName("student_name")
- private String name;
- @SerializedName("student_age")
- private int age;
- // private transient int age;//不参与序列化
- private Score score;
-
- public Student(String name, int age, Score score) {
- this.name = name;
- this.age = age;
- this.score = score;
- }
-
- public String getName() {
- return name;
- }
-
- public int getAge() {
- return age;
- }
-
- public Score getScore() {
- return score;
- }
-
- @Override
- public String toString() {
- return "Student{" + "name='" + name + '\'' + ", age=" + age + ", score=" + score + '}';
- }
- }
- public class Score {
- private int math;
- private int english;
- private int chinese;
-
- public Score(int math, int english, int chinese) {
- this.math = math;
- this.english = english;
- this.chinese = chinese;
- }
-
- public int getMath() {
- return math;
- }
-
- public int getEnglish() {
- return english;
- }
-
- public int getChinese() {
- return chinese;
- }
-
- @Override
- public String toString() {
- return "Score{" + "math=" + math + ", english=" + english + ", chinese=" + chinese + '}';
- }
- }
- //序列化
- private void serialize() {
- Student student1 = new Student("YuYu", 21, new Score(88, 77, 99));
- Gson gson = new Gson();
- String jsonStudent1 = gson.toJson(student1);
- Log.i("MyTag", jsonStudent1);
- }
- //反序列化
- private void deSerialize() {
- String str = "{\"age\":21,\"name\":\"YuYu\",\"score\":{\"chinese\":99,\"english\":77,\"math\":88}}";
- Gson gson = new Gson();
- Student student = gson.fromJson(str, Student.class);
- Log.i("MyTag", student.toString());
- }
- //序列化数组/列表
- private void serializeArray() {
- Student student1 = new Student("AA", 11, new Score(88, 77, 99));
- Student student2 = new Student("BB", 22, new Score(88, 77, 99));
- Student[] students = {student1, student2};
- Gson gson = new Gson();
- String jsonArray = gson.toJson(students);
- Log.i("MyTag", jsonArray);
- }
- //反序列化数组/列表
- private void deSerializeArray() {
- String str = "[{\"age\":11,\"name\":\"AA\",\"score\":{\"chinese\":99,\"english\":77,\"math\":88}}," + "{\"age\":22,\"name\":\"BB\",\"score\":{\"chinese\":99,\"english\":77,\"math\":88}}]";
- Gson gson = new Gson();
- //[] 表示正列
- Student[] students = gson.fromJson(str, Student[].class);
- for (int i = 0; i < students.length; i++) {
- Log.i("MyTag", students[i].toString());
- }
- //数组转List
- List
list = Arrays.asList(students); - }
- //序列化列表
- private void serializeList() {
- Student student1 = new Student("AA", 11, new Score(88, 77, 99));
- Student student2 = new Student("BB", 22, new Score(88, 77, 99));
- //Arrays.asList(student1,student2);
- List
students = new ArrayList<>(); - students.add(student1);
- students.add(student2);
- Gson gson = new Gson();
- String jsonArray = gson.toJson(students);
- Log.i("MyTag", jsonArray);
- }
- //反序列化列表
- private void deSerializeList() {
- String str = "[{\"age\":11,\"name\":\"AA\",\"score\":{\"chinese\":99,\"english\":77,\"math\":88}}," + "{\"age\":22,\"name\":\"BB\",\"score\":{\"chinese\":99,\"english\":77,\"math\":88}}]";
- Gson gson = new Gson();
- Type typeStudents = new TypeToken
>() {
- }.getType();
- List
list = gson.fromJson(str, typeStudents); - for (int i = 0; i < list.size(); i++) {
- Log.i("MyTag", list.get(i).toString());
- }
- }
- //不参与序列化
- @SerializedName("student_age")
- private transient int age;
- private void serializedName() {
- Student student = new Student("WW", 33, new Score(88, 77, 99));
- Gson gson = new Gson();
- String str = gson.toJson(student);
- Log.i("MyTag", str);
- //打印结果
- //{"student_age":33,"student_name":"WW","score":{"chinese":99,"english":77,"math":88}}
- }
- private void deSerializedName() {
- String str = "{\"student_age\":33,\"student_name\":\"WW\",\"score\":{\"chinese\":99,\"english\":77,\"math\":88}}";
- Gson gson = new Gson();
- Student student = gson.fromJson(str, Student.class);
- Log.i("MyTag", student.toString());
- //打印结果 Student{name='WW', age=0, score=Score{math=88, english=77, chinese=99}}
- }