介绍几款常用的Json转换工具
Json序列化与反序列化工具,常用的有jackson、fastjson、Gson。其中fastjson性能最快,但是处理复杂类型的会有bug,我用的jackson,因为无需单独引入依赖,工程中基本会被其他包一起引入。
一、jackson
1、引入依赖
<dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.10.0</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>2.10.0</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.10.0</version> </dependency>
2、使用示例
ObjectMapper mapper = new ObjectMapper(); //序列化 String userStr = mapper.writeValueAsString(user); //反序列化 User user= mapper.readValue(userStr,User.class); //List序列化 List<User> userlist = new ArrayList<>(); ... ... userlist.add(user); userlist.add(user2); String listStr = mapper.writeValueAsString(userlist); //List反序列化(转对象使用TypeReference,否则报LinkedHashMap类型转换错误) List<User> userList = mapper.readValue(listStr,new TypeReference<List<User>>(){}); System.out.println(userList.get(0).getUserName()); //List反序列化(也可使用JavaType) JavaType javaType = mapper.getTypeFactory().constructParametricType(List.class, User.class); List<User> userList = mapper.readValue(listStr, javaType); //Map序列化 Map<String,User> map = new HashMap<>(); ... ... map.put("u1",user); map.put("u2",user2); String mapStr = mapper.writeValueAsString(map); //Map反序列化(使用TypeReference) Map<String,User> tmpMap = mapper.readValue(mapStr, new TypeReference<Map<String,User>>() {}); System.out.println(tmpMap.get("u1").getUserId()); //Map反序列化(也可以使用JavaType) JavaType javaType = mapper.getTypeFactory().constructParametricType(Map.class,String.class,User.class); Map<String,User> tmpMap = mapper.readValue(mapStr, javaType); System.out.println(tmpMap.get("u1").getUserId());
二、fastjson
1、引入依赖
<dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.47</version> </dependency>
2、使用示例
//序列化 String userStr = JSON.toJSONString(user); //反序列化 User user=JSON.parseObject(userStr, User.class); //List序列化 List<User> list = new ArrayList<>(); ... ... list.add(user); list.add(user2); String listStr = JSON.toJSONString(list); //List反序列化 List<User> userList= JSON.parseArray(listStr, User.class); System.out.println(userList.get(0).getUserId()); //Map序列化 Map<String,User> map = new HashMap<>(); ... ... map.put("u1",user); map.put("u2",user2); String mapStr = JSON.toJSONString(map);
//Map反序列化 Map<String,User> tmpMap = JSON.parseObject(mapStr,new TypeReference<Map<String,User>>(){}); System.out.println(tmpMap.get("u1").getUserId());
三、Gson
1、引入依赖
<dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.8.6</version> </dependency>
2、使用示例
Gson gson = new Gson(); //序列化 String userStr = gson.toJson(user); //反序列化 User user = gson.fromJson(userStr,User.class); //List序列化 List<User> list = new ArrayList<>(); ... ... list.add(user); list.add(user2); String listStr = gson.toJson(list); //List反序列化 List<User> userList= gson.fromJson(listStr, new TypeToken<List<User>>(){}.getType()); System.out.println(userList.get(0).getUserId()); //Map序列化 Map<String,User> map = new HashMap<>(); ... ... map.put("u1",user); map.put("u2",user2); String mapStr = gson.toJson(map); //Map反序列化 Map<String,User> map2 = gson.fromJson(mapStr,new TypeToken<Map<String,User>>(){}.getType()); System.out.println(map2.get("u1").getUserId());