Java 使用jackson反序列化为自定义对象的HashMap

Java 使用jackson反序列化为自定义对象的HashMap,java,jackson,json-deserialization,Java,Jackson,Json Deserialization,我有以下课程: import org.codehaus.jackson.annotate.JsonIgnoreProperties; import org.codehaus.jackson.annotate.JsonProperty; import java.io.Serializable; import java.util.HashMap; @JsonIgnoreProperties(ignoreUnknown = true) public class Theme implements S

我有以下课程:

import org.codehaus.jackson.annotate.JsonIgnoreProperties;
import org.codehaus.jackson.annotate.JsonProperty;

import java.io.Serializable;
import java.util.HashMap;

@JsonIgnoreProperties(ignoreUnknown = true)
public class Theme implements Serializable {

    @JsonProperty
    private String themeName;

    @JsonProperty
    private boolean customized;

    @JsonProperty
    private HashMap<String, String> descriptor;

    //...getters and setters for the above properties
}
我的问题是将上面的json字符串反序列化为

HashMap<String, Theme> 
HashMap
反对

我的反序列化代码如下所示:

{
  "theme2": {
    "themeName": "theme2",
    "customized": true,
    "descriptor": {
      "foo": "one",
       "bar": "two"
    }
  },
  "theme1": {
    "themeName": "theme1",
    "customized": false,
    "descriptor": null
  }
}
HashMap<String, Themes> themes =
        objectMapperFactory.createObjectMapper().readValue(json, HashMap.class);
HashMap主题=
objectMapperFactory.createObjectMapper().readValue(json,HashMap.class);
它使用正确的键反序列化为HashMap,但不为值创建主题对象。我不知道应该指定什么来代替readValue()方法中的“HashMap.class”


任何帮助都将不胜感激。

您应该创建特定的映射类型,并将其提供到反序列化过程中:

TypeFactory typeFactory = mapper.getTypeFactory();
MapType mapType = typeFactory.constructMapType(HashMap.class, String.class, Theme.class);
HashMap<String, Theme> map = mapper.readValue(json, mapType);
TypeFactory-TypeFactory=mapper.getTypeFactory();
MapType MapType=typeFactory.constructMapType(HashMap.class、String.class、Theme.class);
HashMap-map=mapper.readValue(json,mapType);

您可以使用TypeReference类,该类对具有用户定义类型的映射执行类型转换。更多文档,请访问

ObjectMapper mapper=new ObjectMapper();
映射结果=
readValue(src,newTypeReference(){});

您可以制作一个扩展地图的POJO

这对于处理对象的嵌套贴图非常重要

{
  key1: { nestedKey1: { value: 'You did it!' } }
}
这可以通过以下方式进行反序列化:

class Parent extends HashMap<String, Child> {}

class Child extends HashMap<String, MyCoolPojo> {}

class MyCoolPojo { public String value; }

Parent parent = new ObjectMapper().readValue(json, Parent.class);
parent.get("key1").get("nestedKey1").value; // "You did it!"
类父级扩展HashMap{}
类子级扩展HashMap{}
类MyCoolPojo{public String value;}
Parent Parent=newObjectMapper().readValue(json,Parent.class);
parent.get(“key1”).get(“nestedKey1”).value;//“你做到了!”

好的,我们的方向肯定是正确的,因为它正在使用上面的代码进行反序列化。但是,hashmap只包含一个键/值(theme2)。它确实有与键关联的正确主题,但theme1不在hashmap中?为什么不在。对我来说,当我反序列化你的JSON时,应用程序会打印这个:{theme1=Theme[themeName=theme1,customized=false,descriptor=null],theme2=theme2,customized=true,descriptor={foo=one,bar=two}}。在我的测试数据中找到了格式错误的JSON字符串。因此,您的解决方案如广告所示有效;)我也可以只使用Jackson注释吗?
{
  key1: { nestedKey1: { value: 'You did it!' } }
}
class Parent extends HashMap<String, Child> {}

class Child extends HashMap<String, MyCoolPojo> {}

class MyCoolPojo { public String value; }

Parent parent = new ObjectMapper().readValue(json, Parent.class);
parent.get("key1").get("nestedKey1").value; // "You did it!"