【问题标题】:Jsonify nested objects using Jackson [duplicate]使用杰克逊对嵌套对象进行 Jsonify [重复]
【发布时间】:2018-02-16 11:29:44
【问题描述】:

我有这样的课:

   class Car {
        private Engine engine;
        private String color;  
        private int maxspeed;
    }

和引擎类类似

class Engine {
    private String fueltype;
    private String enginetype;
}  

我想使用 Jackson 将 Car 对象转换为 JSON,其结构类似于

'car': {
   'color': 'red',
   'maxspeed': '200',
   'fueltype': 'diesel',
   'enginetype': 'four-stroke'
 } 

我该怎么做?

【问题讨论】:

    标签: java json spring rest jackson


    【解决方案1】:

    尝试使用@JsonUnwrapped注解。

    class Car {
        @JsonUnwrapped private Engine engine;
        private String color;  
        private int maxspeed;
    }
    

    相反,使用@JsonCreator@JsonProperty

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-01-14
      • 2016-12-07
      • 2019-11-15
      • 1970-01-01
      • 1970-01-01
      • 2021-10-02
      • 2014-10-12
      相关资源
      最近更新 更多