Map<String, Object> map1 = mapper.readValue(json, new TypeReference<Map<String, Object>>() {});
基本上翻译成,给我返回一个 Map ,其键类型为 String ,值类型为 Object。所以,杰克逊给了你 type 的键和 type 的String
值Object
。Jackson 不知道您的自定义对象,这就是为什么它为您提供了自己的本机边界,具体来说Object
是 a Map
,特别是 a LinkedHashMap
,因此您在对返回的对象LinkedHashMap
执行 a 时得到 a 的原因get
Map
所以将其更改为:
Map<String, IronRunId> map1 = mapper.readValue(json, new TypeReference<Map<String, IronRunId>>() {});
此外,最好声明其接口类型的 Object 而非其具体类型。所以而不是
HashMap<String, Object> map = new HashMap<String, Object>();
做了
Map<String, Object> map = new HashMap<String, Object>();
编辑
作为对您添加的问题的回应,您可以创建一个包装器对象来处理您的所有对象。像这样的东西。
class Wrapper{
private IronRunId ironRunId;
private long time;
private Map<String, String> aspects;
private String anotherString;
public long getTime() {
return time;
}
public void setTime(long time) {
this.time = time;
}
public Map<String, String> getAspects() {
return aspects;
}
public void setAspects(Map<String, String> aspects) {
this.aspects = aspects;
}
public String getAnotherString() {
return anotherString;
}
public void setAnotherString(String anotherString) {
this.anotherString = anotherString;
}
public IronRunId getIronRunId() {
return ironRunId;
}
public void setIronRunId(IronRunId ironRunId) {
this.ironRunId = ironRunId;
}
}
然后,您可以在此类中存储不同的对象。
经过修改的版本
public static void main(String[] args) throws JsonGenerationException, JsonMappingException, IOException{
IronRunId Id = new IronRunId("RunObject", "Runid1", 4);
Map<String, String> aspects = new HashMap<String, String>();
aspects.put("aspectskey1", "aspectsValue1");
aspects.put("aspectskey2", "aspectsValue2");
aspects.put("aspectskey3", "aspectsValue3");
String anotherString = "anotherString";
long time = 1L;
Wrapper objectWrapper = new Wrapper();
ObjectMapper objectMapper = new ObjectMapper();
objectWrapper.setIronRunId(Id);
objectWrapper.setTime(time);
objectWrapper.setAnotherString(anotherString);
objectWrapper.setAspects(aspects);
Map<String, Wrapper> map = new HashMap<String, Wrapper>();
map.put("theWrapper", objectWrapper);
String json = objectMapper.writeValueAsString(map);
Map<String, Wrapper> map1 = objectMapper.readValue(json, new TypeReference<Map<String, Wrapper>>() {});
Wrapper wrapper = map1.get("theWrapper");
System.out.println("run id : " + wrapper.getIronRunId().toString());
System.out.println("time : " + wrapper.getTime());
System.out.println("aspects : " + wrapper.getAspects().toString());
System.out.println("anotherString : " + wrapper.getAnotherString());
}