Gson 序列化内部类

gson 序列化内部类

在本章中,我们将解释具有内部类的类的序列化/反序列化。

 

1. 嵌套的内部类示例

student student = new student();
student.setrollno(1);
student.name name = student.new name();
name.firstname = "mahesh";
name.lastname = "kumar";

student.setname(name);
//serialize inner class object
string namestring = gson.tojson(name);
system.out.println(namestring);

//deserialize inner class object   
name = gson.fromjson(namestring,student.name.class);
system.out.println(name.getclass());

1) 范例

让我们看一个带有内部类的类的序列化/反序列化的示例。在c:> gson_workspace中创建名为 gsontester 的java类文件。

文件:gsontester.java

import com.google.gson.gson;  

public class gsontester {
   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;
   }
}

2) 验证结果

使用 javac 编译器编译类如下 -

c:\gson_workspace>javac gsontester.java

现在运行gsontester查看结果 -

c:\gson_workspace>java gsontester

验证输出。

{"rollno":1,"name":{"firstname":"mahesh","lastname":"kumar"}}
roll no: 1
first name: mahesh
last name: kumar
{"firstname":"mahesh","lastname":"kumar"}
class student$name
first name: mahesh
last name: kumar

 

2. 嵌套静态内部类示例

student student = new student();
student.setrollno(1);
student.name name = new student.name();

name.firstname = "mahesh";
name.lastname = "kumar";
student.setname(name);

//serialize static inner class object
string namestring = gson.tojson(name);
system.out.println(namestring);

//deserialize static inner class object   
name = gson.fromjson(namestring,student.name.class);
system.out.println(name.getclass());

1) 范例

让我们看一个带有静态内部类的类的序列化/反序列化的示例。在c:> gson_workspace中创建名为gsontester的java类文件。

文件:gsontester.java

import com.google.gson.gson;  

public class gsontester {
   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) 验证结果

使用 javac 编译器编译类如下:

c:\gson_workspace>javac gsontester.java

现在运行 gsontester 查看结果:

c:\gson_workspace>java gsontester

验证输出。

{"rollno":1,"name":{"firstname":"mahesh","lastname":"kumar"}}
roll no: 1
first name: mahesh
last name: kumar
{"firstname":"mahesh","lastname":"kumar"}
class student$name
first name: mahesh
last name: kumar

下一节:gson 定制型适配器

gson 教程

相关文章