new TypeReference 问题

Posted wangsong412

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了new TypeReference 问题相关的知识,希望对你有一定的参考价值。

Map<Integer, Long> map = new HashMap<>();
map.put(0, 0L);
map.put(1, 1L);

String a = JSON.toJSONString(map);

map = JSON.parseObject(a, Map.class);
Long b = map.get(1);

  

#########
Exception in thread "main" java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Long
json parse时 强制类型转换
#########
Map<Integer, Long> map = new HashMap<>();
map.put(0, 0L);
map.put(1, 1L);

String a = JSON.toJSONString(map);

map = JSON.parseObject(a, new TypeReference<Map<Integer, Long>>(){});
Long b = map.get(1);
#########
OK!
#########

以上是关于new TypeReference 问题的主要内容,如果未能解决你的问题,请参考以下文章