Gson - 序列化内部类示例

x33g5p2x  于2022-10-07 转载在 其他  
字(2.6k)|赞(0)|评价(0)|浏览(797)

在这篇文章中,我们将讨论如何对具有内部类的类进行序列化/反序列化。

嵌套类的例子

让我们看看一个嵌套类的序列化/反序列化的例子。

  1. package net.javaguides.gson;
  2. import com.google.gson.Gson;
  3. import com.google.gson.GsonBuilder;
  4. /**
  5. *
  6. * @author Ramesh Fadatare
  7. *
  8. */
  9. public class GSONNestedClassesExample {
  10. public static void main(String[] args) {
  11. Gson gson = new GsonBuilder().setPrettyPrinting().create();
  12. OuterClass outerClass = new OuterClass();
  13. outerClass.setValue1("value1");
  14. outerClass.setValue2("value2");
  15. OuterClass.InnerClass innerClass = outerClass.new InnerClass();
  16. innerClass.value3 = "value3";
  17. innerClass.value4 = "value4";
  18. outerClass.setInnerClass(innerClass);
  19. String jsonStr = gson.toJson(outerClass);
  20. System.out.println(jsonStr);
  21. }
  22. }
  23. class OuterClass {
  24. private String value1;
  25. private String value2;
  26. private InnerClass innerClass;
  27. public String getValue1() {
  28. return value1;
  29. }
  30. public void setValue1(String value1) {
  31. this.value1 = value1;
  32. }
  33. public String getValue2() {
  34. return value2;
  35. }
  36. public void setValue2(String value2) {
  37. this.value2 = value2;
  38. }
  39. public void setInnerClass(InnerClass innerClass) {
  40. this.innerClass = innerClass;
  41. }
  42. public InnerClass getInnerClass() {
  43. return innerClass;
  44. }
  45. class InnerClass {
  46. public String value3;
  47. public String value4;
  48. }
  49. }

输出:

  1. {
  2. "value1": "value1",
  3. "value2": "value2",
  4. "innerClass": {
  5. "value3": "value3",
  6. "value4": "value4"
  7. }
  8. }

嵌套静态类实例

让我们看看一个带有静态类的序列化/反序列化的例子。

  1. package net.javaguides.gson;
  2. import com.google.gson.Gson;
  3. import com.google.gson.GsonBuilder;
  4. /**
  5. *
  6. * @author Ramesh Fadatare
  7. *
  8. */
  9. public class GSONNestedClassesExample {
  10. public static void main(String[] args) {
  11. Gson gson = new GsonBuilder().setPrettyPrinting().create();
  12. OuterClass outerClass = new OuterClass();
  13. outerClass.setValue1("value1");
  14. outerClass.setValue2("value2");
  15. OuterClass.InnerClass innerClass = new OuterClass.InnerClass();
  16. innerClass.value3 = "value3";
  17. innerClass.value4 = "value4";
  18. outerClass.setInnerClass(innerClass);
  19. String jsonStr = gson.toJson(outerClass);
  20. System.out.println(jsonStr);
  21. }
  22. }
  23. class OuterClass {
  24. private String value1;
  25. private String value2;
  26. private InnerClass innerClass;
  27. public String getValue1() {
  28. return value1;
  29. }
  30. public void setValue1(String value1) {
  31. this.value1 = value1;
  32. }
  33. public String getValue2() {
  34. return value2;
  35. }
  36. public void setValue2(String value2) {
  37. this.value2 = value2;
  38. }
  39. public void setInnerClass(InnerClass innerClass) {
  40. this.innerClass = innerClass;
  41. }
  42. public InnerClass getInnerClass() {
  43. return innerClass;
  44. }
  45. static class InnerClass {
  46. public String value3;
  47. public String value4;
  48. }
  49. }

输出:

  1. {
  2. "value1": "value1",
  3. "value2": "value2",
  4. "innerClass": {
  5. "value3": "value3",
  6. "value4": "value4"
  7. }
  8. }

相关文章