背景
有个需求需要把Map<Long,Entity>转为Json字符串存入Redis,Map转字符串使用JSON.toJSONString没有问题,但反序列化使用Map<Long,Entity>接收JSON.parseObject(param,Map.class)时,Map的Key却不是Long,而是Interger导致类型转换异常。
另外Entity事实上被序列化为了HashMap。
解决
反序列化改为:
Map<Long,Entity> map = JSON.parseObject(param, new TypeReference<Map<Long,Entity>>() {
});
明确指定具体的反序列化类
网友评论