我有关于JSON反序列化的问题。我有一个这样的课:
public class MapHolder
{
HashMap<String,Integer> map1;
HashMap<String,MyObject> map2
HashMap<Integer, String> map3;
}
如果我使用Jackson和Spring将这个类序列化为Json:
@RequestMapping(value = "/state", method = RequestMethod.GET)
@ResponseBody
public String state() throws Exception
{
ObjectMapper mapper = new ObjectMapper();
mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
return mapper.writeValueAsString(this.mapHolder);
}
它起作用并产生一些Json,如:
{
"map1": [
{ ...
"map2": [
{ ...
}
但是当我想要将生成的Json字符串反序列化回MapHolder时,
ObjectMapper mapper = new ObjectMapper();
return mapper.readValue(json, MapHolder.class);
它崩溃了。任何人都可以告诉我该怎么做才能解决它?
谢谢,
约瑟夫