@JsonUnwrapped注解,注解对应的字段对象上边.
序列化之后,json的层级就只有一层.
做个必须 类似于复制文件夹,扁平化之后,就只复制文件,在目标地方不在创建子文件夹.
原json
{"name":"guofeipeng","password":"123","p":{"age":25,"sex":true,"birthday":"2014-12-22 07-15-29","word":"程序员","pname":"nomouse","salary":"2500.00"}}
扁平化json
{"name":"guofeipeng","password":"123","age":25,"sex":true,"birthday":"2014-12-22 07-16-38","word":"程序 员","pname":"nomouse","salary":"2500.00"}
其中的
prefix
suffix
还可以指定生成的key的前缀和后缀.
用处多为转实体类为出参.
官方注释:
<pre>
public class Parent {
public int age;
public Name name;
}
public class Name {
public String first, last;
}
</pre>
which would normally be serialized as follows (assuming @JsonUnwrapped
had no effect):
<pre>
{
"age" : 18,
"name" : {
"first" : "Joey",
"last" : "Sixpack"
}
}
</pre>
can be changed to this:
<pre>
{
"age" : 18,
"first" : "Joey",
"last" : "Sixpack"
}
</pre>
by changing Parent class to:
<pre>
public class Parent {
public int age;
@JsonUnwrapped
public Name name;
}
</pre>
Annotation can only be added to properties, and not classes, as it is contextual.
<p>
Also note that annotation only applies if
<ul>
<li>Value is serialized as JSON Object (can not unwrap JSON arrays using this
mechanism)
</li>
<li>Serialization is done using <code>BeanSerializer</code>, not a custom serializer
</li>
<li>No type information is added; if type information needs to be added, structure can
not be altered regardless of inclusion strategy; so annotation is basically ignored.
</li>
</ul>