通过Jackson实现Java对象和json字符串的相互转换

来源:互联网 发布:装配式钢结构 知乎 编辑:程序博客网 时间:2024/05/22 01:49

通过Jackson实现Java对象和json字符串的相互转换 

下面的案例使用单元测试(junit)方式进行了测试,所以在测试中需要配置junit环境,本人使用的eclipse开发工具,eclipse自带junit测试环境但需要配置,其它的开发工具则需要导入junit测试包。

注:junit测试时测试的方法名必须以test开始,并且在方法上面必须加 @Test

需要导入的Jackson包:


示例代码如下:

package cn.sz.hcq.test;import java.io.IOException;import java.util.ArrayList;import java.util.HashMap;import java.util.Iterator;import java.util.List;import java.util.Map;import java.util.Set;import org.apache.jasper.tagplugins.jstl.core.ForEach;import org.codehaus.jackson.JsonGenerationException;import org.codehaus.jackson.JsonParseException;import org.codehaus.jackson.map.JsonMappingException;import org.codehaus.jackson.map.ObjectMapper;import org.codehaus.jackson.type.JavaType;import org.codehaus.jackson.type.TypeReference;import org.junit.Test;import cn.sz.hcq.pojo.User;public class JacksonTest {/** * 将java对象转换为json字符串 *  * @throws IOException * @throws JsonMappingException * @throws JsonGenerationException */@Testpublic void testObjectToString() throws JsonGenerationException, JsonMappingException, IOException {User u1 = new User();u1.setId(1);u1.setName("张三");// 创建实现转换的对象ObjectMapper mapper = new ObjectMapper();// 将java对象转换为json字符串String str = mapper.writeValueAsString(u1);System.out.println("将java对象转换为json字符串:" + str);}/** * 将json字符串转换为Java对象 *  * @throws IOException * @throws JsonMappingException * @throws JsonParseException */@Testpublic void testStringToObject() throws JsonParseException, JsonMappingException, IOException {String str = "{\"id\":1,\"name\":\"张三\"}";ObjectMapper mapper = new ObjectMapper();// 将json字符串解析为一个Java对象User u = mapper.readValue(str, User.class);System.out.println("将json字符串转换为Java对象:" + u.toString());}/** * 将List对象转换为Json字符串 *  * @throws JsonGenerationException * @throws JsonMappingException * @throws IOException */@Testpublic void testListToString() throws JsonGenerationException, JsonMappingException, IOException {User u1 = new User();u1.setId(1);u1.setName("张三");User u2 = new User();u2.setId(2);u2.setName("李四");List<User> list = new ArrayList<>();list.add(u1);list.add(u2);ObjectMapper oMapper = new ObjectMapper();String str = oMapper.writeValueAsString(list);System.out.println("将List对象转换为Json字符串:" + str);}/** * 将Json字符串转换为List对象 *  * @throws IOException * @throws JsonMappingException * @throws JsonParseException */@Testpublic void testStringToList() throws JsonParseException, JsonMappingException, IOException {String str = "[{\"id\":1,\"name\":\"张三\"},{\"id\":1,\"name\":\"李四\"}]";ObjectMapper mapper = new ObjectMapper();List<User> list = mapper.readValue(str, getCollectionType(List.class, User.class));for (User u : list) {System.out.println("将Json字符串转换为List对象:" + u);}}public JavaType getCollectionType(Class<?> collectionClass, Class<?>... elementClasses) {ObjectMapper mapper = new ObjectMapper();return mapper.getTypeFactory().constructParametricType(collectionClass, elementClasses);}/** * 将Map对象转换为Json字符串 *  * @throws JsonGenerationException * @throws JsonMappingException * @throws IOException */@Testpublic void testMapToString() throws JsonGenerationException, JsonMappingException, IOException {User u1 = new User();u1.setId(1);u1.setName("张三");User u2 = new User();u2.setId(2);u2.setName("李四");Map<String, User> map = new HashMap<>();map.put("u1", u1);map.put("u2", u2);ObjectMapper oMapper = new ObjectMapper();String str = oMapper.writeValueAsString(map);System.out.println("将Map对象转换为Json字符串:" + str);}/** * 将Json字符串转换为Map对象 *  * @throws IOException * @throws JsonMappingException * @throws JsonParseException */@Testpublic void testStringToMap() throws JsonParseException, JsonMappingException, IOException {String str = "{\"u1\":{\"id\":1,\"name\":\"张三\"},\"u2\":{\"id\":2,\"name\":\"李四\"}}";ObjectMapper mapper = new ObjectMapper();Map<String, User> map = mapper.readValue(str, new TypeReference<Map<String, User>>() {});Set<String> set = map.keySet();Iterator<String> it = set.iterator();while (it.hasNext()) {String key = (String) it.next();System.out.println(key + "," + map.get(key));}}}



测试结果如下:

将Java对象转换为json字符串


将json字符串转换为java对象


将list集合转换为json字符串


将json字符串转换为list集合


将映射集合(Map)转换为json字符串


将json字符串转换为映射集合对象


阅读全文
0 0
原创粉丝点击