时间:2021-05-20
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 序列化的资料请关注其它相关文章!
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
Gson反序列化原理原理简述gson反序列化主要分为两个过程:根据TypeToken创建出对象根据json字符串解析数据,对对象属性赋值对象的创建Constru
Java—IO流对象的序列化和反序列化序列化的基本操作 1.对象序列化,就是将Object转换成byte序列,反之叫对象的反序列化。 2.序列化流(Obje
前言本文准确来讲是探讨如何用Jackson来序列化Apacheavro对象,因为简单用Jackson来序列化Apacheavro对象会报错。原因是序列化Sche
详解Python序列化Serialize和反序列化Deserialize序列化(serialization)序列化是将对象状态转换为可保持或传输的格式的过程。与
1.Java序列化与反序列化Java序列化是指把Java对象转换为字节序列的过程;而Java反序列化是指把字节序列恢复为Java对象的过程。2.为什么需要序列化