Gson 转换返回值类型问题

记录!!!! 记录!!!! 记录!!!!

Gson对含有纯数字的对象转换后可能出现类型问题,比如:

常用的转换方法:

String str = "[{key1=adddd, key2=1}]";
Gson gson = new Gson();
List<Map<String, String>> list = new ArrayList<>();
list = gson.fromJson(str,list.getClass());

返回值就变成了:[{key1=adddd, key2=1.0}]  

 

解决办法:

String str = "[{key1=adddd, key2=1}]";
Gson gson = new Gson();
List<Map<String, String>> list = new ArrayList<>();
list = gson.fromJson(str,new TypeToken<List<Map<String, String>>>(){}.getType());

 

posted @ 2021-03-16 15:48  刘猿外  阅读(189)  评论(0编辑  收藏  举报