【问题标题】:Serialization pojo into different json structure将pojo序列化为不同的json结构
【发布时间】:2016-06-17 06:06:00
【问题描述】:

我需要根据发送请求的对象将 pojo 序列化为不同的 json 结构。此外,我应该能够在一些配置中配置 pojo 的字段如何映射到给定请求的 json 属性。

这可以使用杰克逊实现吗? 是否有一些库或 api 可以做到这一点?

编辑: 例如:

public class Universal {
    private int id;
    private Date date;
    private String name;
    private Inner inner;
    private Map<String,Object> others;
    private List<Inner> inners;

}
public class Inner {
    private String value;
}

现在上面是我需要创建动态 json 的两个对象,下面是一些转换的示例

{ 
  "id":"",//value will be  id of Universal
  "detials":{
       "name":"",//value will be name of Universal
   },
   "data":[], // array of value(field of Inner) from inners 
   "ext":{
      "prop1":""// value of this field will be some (key1) value from others 
    } 
}

【问题讨论】:

    标签: java json serialization jackson


    【解决方案1】:

    您可以使用 Google Gson 并依赖其类型适配器。

    http://www.javacreed.com/gson-typeadapter-example/是一篇来自网络的好文章

    【讨论】:

    • 我想我必须编写类型适配器,它将解析我的配置以转换 jsonElements 中给定的通用对象。我也更新了问题
    猜你喜欢
    • 1970-01-01
    • 2017-08-09
    • 1970-01-01
    • 1970-01-01
    • 2012-02-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多