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中創(chuàng)建名為 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
現(xiàn)在運行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. 嵌套靜態(tài)內部類示例
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) 范例
讓我們看一個帶有靜態(tài)內部類的類的序列化/反序列化的示例。在c:> gson_workspace中創(chuàng)建名為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
現(xiàn)在運行 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
相關文章