• 序列化(三)JSON


    1. JSON 相关说明

      1.1 JSON 全称: JavaScript Object Notation
      1.2 Android studio 中创建JSON文件: File -> New -> Scratch File -> JSON
      1.3 transient 关键字: 不参与序列化
      1.4 @SerializedName 序列化,反序化时,自定义标记的名称

    2. build.gradle 文件引入 GSON 库

    1. dependencies {
    2. implementation 'com.google.code.gson:gson:2.9.1'
    3. }

    3. 创建两个实体类

      3.1 Student.java

    1. public class Student {
    2. @SerializedName("student_name")
    3. private String name;
    4. @SerializedName("student_age")
    5. private int age;
    6. // private transient int age;//不参与序列化
    7. private Score score;
    8. public Student(String name, int age, Score score) {
    9. this.name = name;
    10. this.age = age;
    11. this.score = score;
    12. }
    13. public String getName() {
    14. return name;
    15. }
    16. public int getAge() {
    17. return age;
    18. }
    19. public Score getScore() {
    20. return score;
    21. }
    22. @Override
    23. public String toString() {
    24. return "Student{" + "name='" + name + '\'' + ", age=" + age + ", score=" + score + '}';
    25. }
    26. }

      3.2 Score.java

    1. public class Score {
    2. private int math;
    3. private int english;
    4. private int chinese;
    5. public Score(int math, int english, int chinese) {
    6. this.math = math;
    7. this.english = english;
    8. this.chinese = chinese;
    9. }
    10. public int getMath() {
    11. return math;
    12. }
    13. public int getEnglish() {
    14. return english;
    15. }
    16. public int getChinese() {
    17. return chinese;
    18. }
    19. @Override
    20. public String toString() {
    21. return "Score{" + "math=" + math + ", english=" + english + ", chinese=" + chinese + '}';
    22. }
    23. }

    4. 基本序列化

      4.1 对 Student 序列化

    1. //序列化
    2. private void serialize() {
    3. Student student1 = new Student("YuYu", 21, new Score(88, 77, 99));
    4. Gson gson = new Gson();
    5. String jsonStudent1 = gson.toJson(student1);
    6. Log.i("MyTag", jsonStudent1);
    7. }

      4.2 对 String 反序列化

    1. //反序列化
    2. private void deSerialize() {
    3. String str = "{\"age\":21,\"name\":\"YuYu\",\"score\":{\"chinese\":99,\"english\":77,\"math\":88}}";
    4. Gson gson = new Gson();
    5. Student student = gson.fromJson(str, Student.class);
    6. Log.i("MyTag", student.toString());
    7. }

    5. 序列化数组

      5.1 Student 数组序列化

    1. //序列化数组/列表
    2. private void serializeArray() {
    3. Student student1 = new Student("AA", 11, new Score(88, 77, 99));
    4. Student student2 = new Student("BB", 22, new Score(88, 77, 99));
    5. Student[] students = {student1, student2};
    6. Gson gson = new Gson();
    7. String jsonArray = gson.toJson(students);
    8. Log.i("MyTag", jsonArray);
    9. }

      5.2 String 数组反序列化

    1. //反序列化数组/列表
    2. private void deSerializeArray() {
    3. String str = "[{\"age\":11,\"name\":\"AA\",\"score\":{\"chinese\":99,\"english\":77,\"math\":88}}," + "{\"age\":22,\"name\":\"BB\",\"score\":{\"chinese\":99,\"english\":77,\"math\":88}}]";
    4. Gson gson = new Gson();
    5. //[] 表示正列
    6. Student[] students = gson.fromJson(str, Student[].class);
    7. for (int i = 0; i < students.length; i++) {
    8. Log.i("MyTag", students[i].toString());
    9. }
    10. //数组转List
    11. List list = Arrays.asList(students);
    12. }

    6. 序列化 List

      6.1 Student List 序列化

    1. //序列化列表
    2. private void serializeList() {
    3. Student student1 = new Student("AA", 11, new Score(88, 77, 99));
    4. Student student2 = new Student("BB", 22, new Score(88, 77, 99));
    5. //Arrays.asList(student1,student2);
    6. List students = new ArrayList<>();
    7. students.add(student1);
    8. students.add(student2);
    9. Gson gson = new Gson();
    10. String jsonArray = gson.toJson(students);
    11. Log.i("MyTag", jsonArray);
    12. }

      6.2 String 到 List 反序列化

    1. //反序列化列表
    2. private void deSerializeList() {
    3. String str = "[{\"age\":11,\"name\":\"AA\",\"score\":{\"chinese\":99,\"english\":77,\"math\":88}}," + "{\"age\":22,\"name\":\"BB\",\"score\":{\"chinese\":99,\"english\":77,\"math\":88}}]";
    4. Gson gson = new Gson();
    5. Type typeStudents = new TypeToken>() {
    6. }.getType();
    7. List list = gson.fromJson(str, typeStudents);
    8. for (int i = 0; i < list.size(); i++) {
    9. Log.i("MyTag", list.get(i).toString());
    10. }
    11. }

    7. 序列化自定义

    1. //不参与序列化
    2. @SerializedName("student_age")
    3. private transient int age;

      7.1 序列化自定义标记名称 @SerializedName,transient 不参与序列化

    1. private void serializedName() {
    2. Student student = new Student("WW", 33, new Score(88, 77, 99));
    3. Gson gson = new Gson();
    4. String str = gson.toJson(student);
    5. Log.i("MyTag", str);
    6. //打印结果
    7. //{"student_age":33,"student_name":"WW","score":{"chinese":99,"english":77,"math":88}}
    8. }

      7.2 反序列化自定义标记名称 @SerializedName,transient 不参与序列化

    1. private void deSerializedName() {
    2. String str = "{\"student_age\":33,\"student_name\":\"WW\",\"score\":{\"chinese\":99,\"english\":77,\"math\":88}}";
    3. Gson gson = new Gson();
    4. Student student = gson.fromJson(str, Student.class);
    5. Log.i("MyTag", student.toString());
    6. //打印结果 Student{name='WW', age=0, score=Score{math=88, english=77, chinese=99}}
    7. }

  • 相关阅读:
    为何面试官总是将你简历上的技术问题问到回答不出来为止?
    Instant,LocalDate,LocalDateTime,Date,String之间转换
    js方法:函数作为对象的属性
    植被冠层参数计算软件CAN-EYE的下载与安装方法
    爬虫项目(四):抓取网页所有图片
    Mac环境下反编译apk
    ONLYOFFICE 桌面编辑器 v8.0 更新内容详细攻略
    Css 3 动画
    四十九.强化学习基础
    微信小程序(3)- 小程序样式和组件
  • 原文地址:https://blog.csdn.net/u011193452/article/details/126970783