我使用它来将 JAXB bean 转换为 JSON 代码:
private String marshall(final Book beanObject) throws Exception
{
JAXBContext context = JAXBContext.newInstance(Book.class);
Marshaller marshaller = context.createMarshaller();
Configuration config = new Configuration();
MappedNamespaceConvention con = new MappedNamespaceConvention(config);
StringWriter jsonDocument = new StringWriter();
XMLStreamWriter xmlStreamWriter = new MappedXMLStreamWriter(con, jsonDocument);
marshaller.marshal(beanObject, xmlStreamWriter);
return jsonDocument.toString();
}
对于我的 Book 类,输出是:
{"bookType":{"chapters":["Genesis","Exodus"],"name":"The Bible","pages":600}}
但是,我希望输出与 Jersey 兼容:
{"chapters":["Genesis","Exodus"],"name":"The Bible","pages":600}
如何使用上述代码归档第二个 JSON 表示法并摆脱根元素?
我的解决方案:
现在切换到杰克逊,在那里你可以设置一个根解包选项。不过,如果有的话,我仍然对 Jettison 解决方案感兴趣。