ObjectMapper工具包
- ObjectMapper类,是jackson包下一个类,java针对处理对象与json之间关系转换用的
- 需要引入jar包
import java.io.IOException;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.SerializationConfig;
- 对应maven依赖
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.8.3</version>
</dependency>
- 封装对象User
package com.entity;
import java.io.Serializable;
public class User implements Serializable {
private static final long serialVersionUID = 1L;
private String userName;
private String age;
private String sex;
private String email;
private String phone;
public User() {
super();
}
public User(String userName, String age, String sex, String email, String phone) {
super();
this.userName = userName;
this.age = age;
this.sex = sex;
this.email = email;
this.phone = phone;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
@Override
public String toString() {
return "User{" +
"userName='" + userName + '\'' +
", age='" + age + '\'' +
", sex='" + sex + '\'' +
", email='" + email + '\'' +
", phone='" + phone + '\'' +
'}';
}
}
- ObjectMapper 对象预处理
public static ObjectMapper mapper = new ObjectMapper();
//涉及到一下问题需要预处理,否则直接在引用时new new ObjectMapper();即可
static {
// 转换为格式化的json
mapper .enable( SerializationFeature.INDENT_OUTPUT);
// 如果json中有新增的字段并且是实体类类中不存在的,不报错
mapper .configure( DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
}
-
ObjectMapper -> write (写)
//将对象信息写入指定文件
mapper.writeValue(new File("C:\\Users\\Chyb\\Desktop\\新建文本文档.txt"), user);
//将对象信息写到控制台
mapper.writeValue(System.out, user);
//将Map对象转换为JSON字符串(对象为Object对象,均可以转换为JOSN串)
String str = mapper.writeValueAsString( map )
String str = objectMapper.writeValueAsString( list );
//将对象转换为JSON串,依赖于对象中封装的get、set方法(没有get、set方法会报JSON转换异常)
String userJSON = mapper.writeValueAsString( user );
//将对象转换为byte数组
byte[] bytes = objectMapper.writeValueAsBytes( u );
//如果转换的对象中有日期元素,在mapper对象中填装时间格式化属性,那么接下来用mapper对下对象转换过的对象,转换后时间元素会被格式化,如果在填装时间格式化属性之前则不会格式化生效
mapper.setDateFormat( new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
-
ObjectMapper -> read (读)
//读取JSON数据转换实体对象,依赖于实体对象的无参构造方法,无参构造方法不存在则会报JSON序列化异常
User u = mapper.readValue( userJSON, User.class );
//读取文件内容,转换为实体对象
User fileUser = objectMapper.readValue( new File( "C:\\Users\\Chyb\\Desktop\\新建文本文档.txt" ), User.class );
//读取文件内容,转换为Map对象
Map map = objectMapper.readValue( new File( "C:\\Users\\Chyb\\Desktop\\新建文本文档.txt" ), Map.class );
//读取byte数组,填装至实体对象
User user = objectMapper.readValue( bytes, User.class );
chenyb 随笔记录,方便自己使用
2018-10-30