码迷,mamicode.com
首页 > 其他好文 > 详细

new TypeReference 问题

时间:2019-11-04 19:19:03      阅读:198      评论:0      收藏:0      [点我收藏+]

标签:read   integer   object   shm   转换   string   highlight   reference   new t   

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 问题

标签:read   integer   object   shm   转换   string   highlight   reference   new t   

原文地址:https://www.cnblogs.com/wangsong412/p/11794034.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!