Gson如何序列化内部类
作者:一点教程
本文介绍如何使用Gson对成员内部类及静态内部类进行序列化/反序列化,帮助大家更好的理解和使用gson库,感兴趣的朋友可以了解下
1 Gson处理成员内部类
1.1 编写核心类
MainApp:
package com.yiidian.gson; import com.google.gson.Gson; public class MainApp { public static void main(String args[]) { Student student = new Student(); student.setRollNo(1); Student.Name name = student.new Name(); name.firstName = "Mahesh"; name.lastName = "Kumar"; student.setName(name); Gson gson = new Gson(); //序列化 String jsonString = gson.toJson(student); System.out.println(jsonString); //反序列化 student = gson.fromJson(jsonString, Student.class); System.out.println("Roll No: "+ student.getRollNo()); System.out.println("First Name: "+ student.getName().firstName); System.out.println("Last Name: "+ student.getName().lastName); String nameString = gson.toJson(name); System.out.println(nameString); name = gson.fromJson(nameString,Student.Name.class); System.out.println(name.getClass()); System.out.println("First Name: "+ name.firstName); System.out.println("Last Name: "+ name.lastName); } } class Student { private int rollNo; private Name name; public int getRollNo() { return rollNo; } public void setRollNo(int rollNo) { this.rollNo = rollNo; } public Name getName() { return name; } public void setName(Name name) { this.name = name; } //成员内部类 class Name { public String firstName; public String lastName; } }
1.2 运行测试
2 Gson处理静态内部类
2.1 编写核心类
MainApp:
package com.yiidian.gson; import com.google.gson.Gson; public class MainApp { public static void main(String args[]) { Student student = new Student(); student.setRollNo(1); Student.Name name = new Student.Name(); name.firstName = "Mahesh"; name.lastName = "Kumar"; student.setName(name); Gson gson = new Gson(); String jsonString = gson.toJson(student); System.out.println(jsonString); student = gson.fromJson(jsonString, Student.class); System.out.println("Roll No: "+ student.getRollNo()); System.out.println("First Name: "+ student.getName().firstName); System.out.println("Last Name: "+ student.getName().lastName); String nameString = gson.toJson(name); System.out.println(nameString); name = gson.fromJson(nameString,Student.Name.class); System.out.println(name.getClass()); System.out.println("First Name: "+ name.firstName); System.out.println("Last Name: "+ name.lastName); } } class Student { private int rollNo; private Name name; public int getRollNo() { return rollNo; } public void setRollNo(int rollNo) { this.rollNo = rollNo; } public Name getName() { return name; } public void setName(Name name) { this.name = name; } //静态内部类 static class Name { public String firstName; public String lastName; } }
2.2 运行测试
以上就是Gson如何序列化内部类的详细内容,更多关于gson 序列化的资料请关注脚本之家其它相关文章!
您可能感兴趣的文章:
- gson对象序列化的示例
- kotlin gson反序列化默认值失效深入讲解
- java如何利用FastJSON、Gson、Jackson三种Json格式工具自定义时间序列化
- GSON实现Java对象的JSON序列化与反序列化的实例教程
- Java中Gson的使用详解
- Java实现操作JSON的便捷工具类完整实例【重写Google的Gson】
- Java中利用gson解析Json实例教程
- Gson解析空字符串发生异常的处理方法
- Android利用Gson解析嵌套多层的Json的简单方法
- GSON实现Java对象与JSON格式对象相互转换的完全教程
- 举例讲解Java的JSON类库GSON的基本用法
- Java的JSON格式转换库GSON的初步使用笔记