软件测试学习之路

JackJson 的使用

2018-01-06  本文已影响243人  乘风破浪的姐姐

需要包:
jackson-core-2.2.3.jar(核心jar包)
jackson-annotations-2.2.3.jar(该包提供Json注解支持)
jackson-databind-2.2.3.jar

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-core</artifactId>
    <version>2.9.1</version>
</dependency>
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-annotations</artifactId>
    <version>2.9.1</version>
</dependency>
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.9.1</version>
</dependency>
1、指定对象Class转成 json字符串
User user = new User();
user.setName("小民");
user.setEmail("xiaomin@sina.com");
user.setAge(20);

SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd");
user.setBirthday(dateformat.parse("1996-10-01"));

/**

//User类转JSON

{"name":"小民","age":20,"birthday":844099200000,"email":"xiaomin@sina.com"}
String json = mapper.writeValueAsString(user);
System.out.println(json);

输出:
{"name":"小民","age":20,"birthday":844099200000,"email":"xiaomin@sina.com"}

2、List集合转化成json字符串
List<User> users = new ArrayList<User>();
users.add(user);
String jsonlist = mapper.writeValueAsString(users);
System.out.println(jsonlist);

输出:

[{"name":"小民","age":20,"birthday":844099200000,"email":"xiaomin@sina.com"}]
3、Json字符串转化成指定Class类
String json = "{\"name\":\"小民\",\"age\":20,\"birthday\":844099200000,\"email\":\"xiaomin@sina.com\"}";
/**
 * ObjectMapper支持从byte[]、File、InputStream、字符串等数据的JSON反序列化。
 */
ObjectMapper mapper = new ObjectMapper();
User user = mapper.readValue(json, User.class);
System.out.println(user);

输出:

User{name='小民aa', age=25, birthday=Tue Oct 01 00:00:00 CST 1996, email='xiaomin@sina.com'}
4、Json字符串转化成集合List

方法一:

String jsonString="[{'id':'1'},{'id':'2'}]";
ObjectMapper mapper = new ObjectMapper();
JavaType javaType = mapper.getTypeFactory().constructParametricType(List.class, Bean.class);
//如果是Map类型  mapper.getTypeFactory().constructParametricType(HashMap.class,String.class, Bean.class);  
List<Bean> lst =  (List<Bean>)mapper.readValue(jsonString, javaType);

输出:

Student{name='s1', age=12, date=Thu Jun 09 20:38:37 CST 2016}
Student{name='s2', age=12, date=Thu Jun 09 20:38:37 CST 2016}
Student{name='s3', age=12, date=Thu Jun 09 20:38:37 CST 2016}
Student{name='s4', age=12, date=Thu Jun 09 20:38:37 CST 2016}
Student{name='s5', age=12, date=Thu Jun 09 20:38:37 CST 2016}
Student{name='s6', age=12, date=Thu Jun 09 20:38:37 CST 2016}

方法二:

String jsonString="[{'id':'1'},{'id':'2'}]";
ObjectMapper mapper = new ObjectMapper();
List<Bean> beanList = mapper.readValue(jsonString, new TypeReference<List<Bean>>() {});

输出:

Student{name='s1', age=12, date=Thu Jun 09 20:38:37 CST 2016}
Student{name='s2', age=12, date=Thu Jun 09 20:38:37 CST 2016}
Student{name='s3', age=12, date=Thu Jun 09 20:38:37 CST 2016}
Student{name='s4', age=12, date=Thu Jun 09 20:38:37 CST 2016}
Student{name='s5', age=12, date=Thu Jun 09 20:38:37 CST 2016}
Student{name='s6', age=12, date=Thu Jun 09 20:38:37 CST 2016}
上一篇下一篇

猜你喜欢

热点阅读