Gson序列化內部類

在本章中,我們將解釋具有內部類的類的序列化/反序列化。

嵌套內部類示例

參考以下示例代碼片段 -

Student student = new Student(); 
student.setRollNo(1); 
Student.Name name = student.new Name(); 
name.firstName = "Max"; 
name.lastName = "Su"; 

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());

示例

讓我們來看看一個內部類的序列化/反序列化。 創建一個名爲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; 
   } 
}

執行上面示例代碼,得到以下結果 -

{"rollNo":1,"name":{"firstName":"Max","lastName":"Su"}} 
Roll No: 1 
First Name: Max 
Last Name: Su 
{"firstName":"Max","lastName":"Su"} 
class Student$Name 
First Name: Max 
Last Name: Su

嵌套的靜態內部類示例

參考以下代碼實現 -

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());

示例

我們來看一個實例,其中包含一個靜態內部類的序列化/反序列化。 創建一個名爲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 = "Max"; 
      name.lastName = "Su";  
      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; 
   } 
} 
Verify the resul

執行上面示例代碼,得到以下輸出結果 -

{"rollNo":1,"name":{"firstName":"Max","lastName":"Su"}} 
Roll No: 1 
First Name: Max 
Last Name: Su 
{"firstName":"Max","lastName":"Su"} 
class Student$Name 
First Name: Max 
Last Name: Su
0 條評論,你可以發表評論,我們會進行改進
Comment author placeholder