Jackson官网 http://wiki.fasterxml.com/
Jackson下载 https://github.com/FasterXML/jackson-annotations/wiki
本文代码下载 https://pan.baidu.com/s/1dEHqPlZ
本文介绍Jackson简单应用,希望能对大家有用,操作流程如下:
第一步:创建Maven项目,配置pom.xml。
<dependencies> <!-- Jackson包 --> <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> </dependencies>
第二步:创建测试文件,功能测试。
Users对象
package com.learn.jackson; public class Users { private String uuid; private String name; private Integer age; private Integer sex; private String remark; public Users() { } public Users(String uuid,String name,Integer age, Integer sex, String remark){ this.uuid = uuid; this.name = name; this.age = age; this.sex = sex; this.remark = remark; } public String getUuid() { return uuid; } public void setUuid(String uuid) { this.uuid = uuid; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public Integer getSex() { return sex; } public void setSex(Integer sex) { this.sex = sex; } public String getRemark() { return remark; } public void setRemark(String remark) { this.remark = remark; } public String getMassages(){ return uuid+"--"+name+"--"+sex+"--"+age+"--"+remark; } }
测试文件
package com.learn.jackson; import java.io.IOException; import java.util.ArrayList; import java.util.LinkedHashMap; import java.util.List; import junit.framework.TestCase; import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.ObjectMapper; public class AppTest extends TestCase { public static void main(String[] args) throws IOException { //测试数据 Users user = new Users("aaa", "aaa", 23, 1, "这是aaa"); List<Users> list = new ArrayList<Users>(); list.add(new Users("bbb", "bbb", 45, 1, "这是bbb")); list.add(new Users("ccc", "ccc", 23, 1, "这是ccc")); //Jackson对象 ObjectMapper mapper = new ObjectMapper(); mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); System.out.println("********JSON -> 对象************"); String jsonUser = mapper.writeValueAsString(user); String jsonList = mapper.writeValueAsString(list); System.out.println(jsonUser); System.out.println(jsonList); System.out.println("********对象 -> JSON************"); Users us = mapper.readValue(jsonUser, Users.class); System.out.println(us.getMassages()); List<LinkedHashMap> lis = mapper.readValue(jsonList, List.class); for (LinkedHashMap map : lis) { String json = mapper.writeValueAsString(map); Users uu1 = mapper.readValue(json, Users.class); System.out.println(uu1.getMassages()); } } }
异常处理:
JSON转实体抛出异常com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException,是由于实体中缺少JSON的某个字段引起。可通过如下配置解决:
1.通过mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)配置实现。
2.通过@JsonIgnoreProperties(ignoreUnknown = true)注解实现