杰克逊:序列化对象时

时间:2019-07-26 09:58:07

标签: java json jackson

我要序列化以下形式的pojo:

public class Person
{
   private String firstName;

   private String lastName;

   private Map<String, String> details;
}

我正在使用地图,因为我没有很多额外的细节。 因此,例如,如果我将类实例化为:

Map<String, String> details = new HashMap<>();
details.put("age", "20");
details.put("favouriteAnimal", "Unicorn");
Person person = new Person("A", "B", details);

我希望json序列化看起来像:

{
  "person": {
    "firstName": "A",
    "lastName": "B",
    "age": "20, 
    "favouriteAnimal": "Unicorn" 
  } 
}

使用Jackson,“默认”序列化格式为:

{
  "person": {
    "firstName": "A",
    "lastName": "B",
    "details": {
      "age": "20", 
      "favouriteAnimal": "Unicorn" 
    }
  } 
}

0 个答案:

没有答案