ublic class Test {
public static void main(String[] args) {
Map<String, User> map = new HashMap<>();
User user = new User();
(11);
(11);
("1",user);
String jsonString = (map);
String str = new String(());
/**
* fastjson
*/
Map<String,User> map1 = (jsonString, );
("FastJson直接解析:"+ ("1") );
Map<String,User> map2 = (str, new TypeReference<Map<String,User>>(){}.getType());
("FastJson包装解析:" + ("1"));
/**
* gson
*/
Gson gson = new Gson();
Map<String,User> map3 = (str, );
("gson直接解析:"+ ("1"));
Map<String,User> map4 = (str, new TypeToken<Map<String,User>>(){}.getType());
("gson包装解析:"+ ("1"));
//直接解析的没法转换成对象
User user3 = ("1");
(user3);
User user1 = ("1");
(user1);
User user2 = ("1");
(user2);
}
}
执行结果
FastJson直接解析:{"id":11,"age":11}
FastJson包装解析:User{id=11, name='null', age=11}
gson直接解析:{age=11.0, id=11.0}
gson包装解析:User{id=11, name='null', age=11}
Exception in thread "main" : cannot be cast to
User{id=11, name='null', age=11}
at (:45)
直接解析的并没有转换成对象, 所以后面从map里拿取对象的时候 会报转换异常