jackson 序列化成扁平化

@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>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值