假设我有以下三个类(为简洁起见,省略了 getter 和 setter):
@JsonAutoDetect
public class InfoCollection{
    private InfoType1 info1;
    private InfoType2 info2;
}
@JsonAutoDetect
public class InfoType1{
    private String fieldA;
}
@JsonAutoDetect
public class InfoType2{
    private String fieldB;
}
我正在尝试编写一个以这种格式JsonSerializer.serialize()序列化对象的函数:InfoCollection
{
    "allInfo":{
        "fieldA":"foo",
        "fieldB":"bar"
    }
}
这就是我现在所拥有的:
jsonGenerator.writeStartObject();
jsonGenerator.writeFieldName("allInfo");
jsonGenerator.writeObject(myInfoCollection.getInfo1());
jsonGenerator.writeObject(myInfoCollection.getInfo2());
jsonGenerator.writeEndObject();
这导致以下异常:
 org.codehaus.jackson.JsonGenerationException: Can not start an object, expecting field name
我错过了一些小东西还是我完全走错了路?
注意:到目前为止,一些建议的解决方案涉及编写InfoType1和的每个单独字段InfoType2。我正在寻找一个不需要这个的解决方案,因为我想在具有许多字段的大型类上使用该解决方案。