如何使用Jackson在JSON中将Map作为Object属性序列化?

时间:2016-11-15 19:02:28

标签: java json

我有一个Object,它有一些属性和一个未知内容的Map。当我序列化它时,我想从Map中展平键值对,使它们显示为MyObject的属性。下面的代码给出了第一个Json。但我想输出第二个。

{  
   "MyObject":{  
      "name":"name",
      "id":"id",
      "other":{  
         "key1":"value1",
         "key2":"value2"
      }
   }
}

{  
   "MyObject":{  
      "name":"name",
      "id":"id",
      "key1":"value1",
      "key2":"value2"
   }
}


ObjectMapper objectMapper = new ObjectMapper();
return objectMapper.writeValueAsString(this);

public class JsonParent {
  private TopLevel topLevel;

  JsonProperty("MyObject")
  public TopLevel getTopLevel() {
     return topLevel;
  }
} 

public class TopLevel {
  private String id;
  private String name;
  private HashMap<String, String> other;

  public getId()

  public getName()

  public getOther() 
}

0 个答案:

没有答案