将POJO反序列化为HashMap中的值

时间:2014-02-12 14:39:03

标签: java json jackson

我想反序列化一个hashmap,其值是对象的集合/列表。但杰克逊不承认这些物体的类别。

ObjectMapper mapper = new ObjectMapper();
String json = mapper.writeValueAsString(createTestData(1, 0));
System.out.println(json);

这给了我:

{
   "interval":[
      {
         "discriminator":"Interval",
         "uuid":"298d8bc0-4259-4437-bd03-ffa83b62851c",
      }
   ]
}

现在,我想再次反序列化它:

ObjectMapper objectMapper = new ObjectMapper();
HashMap jsonWrapper = objectMapper.readValue(json, HashMap.class);

JacksonInterval对象创建为HashMap。我已将Interval配置为:

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "discriminator")
public class Interval {
    ....
}

0 个答案:

没有答案