我在写一个函数,它使用Gson将Java对象序列化为Json。使用Gson序列化Java对象的字段对象
我的问题是,它只序列化我的类的原始字段,但不是对象字段。例如。我有两个类,如:
class TestClass {
public int i = 10;
public TestClass2 tc2;
}
class TestClass2 {
public int j = 20;
}
而且我的测试是:
@Test
public void shouldSerializeSimpleObjectIntoJson() {
TestClass tc = new TestClass();
String json = new Gson().toJson(tc);
System.out.println(json);
}
JSON的输出是:
{"i":10}
它不含有TC2的领域。
如何配置Gson以递归方式将对象编码为Json?
感谢
有'tc2'是'null'以外的东西。在你的例子中没有任何东西需要编码。 –