ObjectMapper类
Posted Good
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ObjectMapper类相关的知识,希望对你有一定的参考价值。
前言
ObjectMapper类是Jackson库的主要类。它提供一些功能将转换成Java对象匹配JSON结构,反之亦然
使用
使用Jackson,首先需要相关的jar包。对于使用maven的,需要添加以下依赖:
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.9.5</version> </dependency> <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.9.5</version> </dependency> <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-annotations --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>2.9.5</version> </dependency>
实例演示
Java对象转换为JSON对象 json对象转化为java 对象
ObjectMapper objectMapper = new ObjectMapper(); //序列化的时候序列对象的所有属性 objectMapper.setSerializationInclusion(Include.ALWAYS); //反序列化的时候如果多了其他属性,不抛出异常 objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); //如果是空对象的时候,不抛异常 objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false); //取消时间的转化格式,默认是时间戳,可以取消,同时需要设置要表现的时间格式 objectMapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false); objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"))
PS:还需要注意,如果ObjectMapper的configure设置FAIL_ON_EMPTY_BEANS为false,那么对应的实体类的属性没有get方法也不会抛出异常,但是这个属性默认的是true,即必须要有get方法,这个需要注意。
public class ObjectMapperTest { public static void main(String[] args) throws IOException { ObjectMapper objectMapper = new ObjectMapper(); objectMapper.setSerializationInclusion(JsonInclude.Include.ALWAYS); objectMapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, true); Person person = new Person(1, "tom", "123"); String jsonString = objectMapper.writeValueAsString(person); System.out.println("JsonString: " + jsonString); Person person1 = objectMapper.readValue(jsonString, Person.class); System.out.println(person1.toString()); } }
Java数组对象和JSON数组对象之间的转换
public static void getListData() { ObjectMapper objectMapper = new ObjectMapper(); //Java数组转换为JSON数组 Person person = new Person(1, "tom", "123"); Person person1 = new Person(2, "jack", "123445"); List<Person> personList = new ArrayList<>(); personList.add(person); personList.add(person1); String jsonString = null; try { //Json数组转换为Java数组 jsonString = objectMapper.writeValueAsString(personList); System.out.println("JsonString List: " + jsonString); //JavaType JavaType javaType = objectMapper.getTypeFactory().constructParametricType(List.class, Person.class); List<Person> list = objectMapper.readValue(jsonString, javaType); //打印出list中的值 for (Person person2 : list) { System.out.println(person2.toString()); } } catch (Exception e) { e.printStackTrace(); } }
以上是关于ObjectMapper类的主要内容,如果未能解决你的问题,请参考以下文章
swift3中带有objectmapper的alamofire包装类问题
如何使用具有类的通用类型的 ObjectMapper 反序列化 JsonString