是否有杰克逊注释来抑制不必要的JSON包装? [英] Is there a jackson annotation to suppress unnecessary wrapping of JSON?
问题描述
我正在序列化的课程:
public class LogsDTO {
/** The logs. */
private List<LogDTO> logs;
/** Meta data. */
private Meta meta = new Meta();
// more
}
生成的JSON:
{"LogsDTO":{"logs":[{"id":11,"archived":false}],"meta":{"totalPages":0}}}
我希望我的JSON看起来喜欢:
I'd like my JSON to look like:
{"logs":[{"id":11,"archived":false}],"meta":{"totalPages":0}}
有没有办法进行注释以便发生这种情况?
Is there a way to annotate so that this happens?
谢谢
推荐答案
@JsonRootName
:类注释用于指示用于根值的包装器条目的名称,如果启用了根包装。
@JsonRootName
: class annotation used to indicate name of "wrapper" entry used for root value, if root-wrapping is enabled.
在jackson docs中说:< a href =https://github.com/FasterXML/jackson-annotations/wiki/Jackson-Annotations\"rel =nofollow> https://github.com/FasterXML/jackson-annotations/wiki/Jackson-Annotations
Says in jackson docs : https://github.com/FasterXML/jackson-annotations/wiki/Jackson-Annotations
相关的Jira任务: http ://jira.codehaus.org/browse/JA CKSON-630 及以上版本支持它。
Related Jira Task: http://jira.codehaus.org/browse/JACKSON-630 1.9 and above versions supports it.
他们评论了alwaysWrap方法。
They commented alwaysWrap method.
/ *
*可选的标记属性,可定义为true
强制
*包装根元素,无论全局
*root wrap是否启用。
*
*注意false
的值是指使用默认值,
*并且不会阻止使用如果使用由全局功能指示,则为包装器。
*
* @since 2.4
public boolean alwaysWrap()default false;
* /
/* * Optional marker property that can be defined as
true
to force * wrapping of root element, regardless of whether globally * "root wrapping" is enabled or not. ** Note that value of
false
is taken to mean "use defaults", * and will not block use of wrapper if use is indicated by global features. * * @since 2.4 public boolean alwaysWrap() default false; */
他们有计划在v2.5上激活它
They have a plan to activate it on v2.5
从2.4开始,一个缺失的特性是属性alwaysWrap,希望*在2.5中添加,并将用于强制各个类型的根名称包装*。 / p>
As of 2.4, one missing feature is property "alwaysWrap", which is hoped * to be added in 2.5, and would be used to force root name wrapping * for individual types.
这篇关于是否有杰克逊注释来抑制不必要的JSON包装?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!