在项目开发过程中,当客户端与服务器响应时,数据交互是必不可少的。然而通过Json实现数据交互成为我们开发中的一部分,进而Jackson为我们的Json转化提供了很好的机制。下面我将利用实例总结如何使用jackson.
如果你需要使用jackson,你必须得导入相应的架包,有如下三个包
jackson-annotations;jackson-core;jackson-databind
Maven引入依赖代码
com.fasterxml.jackson.core
jackson-databind
2.0.6
com.fasterxml.jackson.module
jackson-module-jaxb-annotations
2.0.6
com.fasterxml.jackson.core
jackson-core
2.3.0
com.fasterxml.jackson.core
jackson-annotations
2.3.0
package com.jackson.bean;
import java.util.List;
public class DeptBean {
private int deptId;
private String deptName;
private List userBeanList;
public int getDeptId() {
return deptId;
}
public void setDeptId(int deptId) {
this.deptId = deptId;
}
public String getDeptName() {
return deptName;
}
public void setDeptName(String deptName) {
this.deptName = deptName;
}
public List getUserBeanList() {
return userBeanList;
}
public void setUserBeanList(List userBeanList) {
this.userBeanList = userBeanList;
}
@Override
public String toString() {
String userBeanListString = "";
for (UserBean userBean : userBeanList) {
userBeanListString += userBean.toString() + "\n";
}
return "DeptBean [deptId=" + deptId + ", deptName=" + deptName
+ ", \nuserBeanListString=" + userBeanListString + "]";
}
public DeptBean(int deptId, String deptName, List userBeanList) {
super();
this.deptId = deptId;
this.deptName = deptName;
this.userBeanList = userBeanList;
}
public DeptBean() {
super();
}
}
package com.jackson.bean;
public class UserBean {
private int userId;
private String userName;
private String password;
private String email;
public int getUserId() {
return userId;
}
public void setUserId(int userId) {
this.userId = userId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@Override
public String toString() {
return "UserBean [userId=" + userId + ", userName=" + userName
+ ", password=" + password + ", email=" + email + "]";
}
public UserBean(int userId, String userName, String password, String email) {
super();
this.userId = userId;
this.userName = userName;
this.password = password;
this.email = email;
}
public UserBean() {
super();
}
}
注意:在实体中必须存在无参的构造方法,否则转换时会有如下异常;
com.fasterxml.jackson.databind.JsonMappingException: No suitable constructor found for type [simple type, class com.jackson.bean.UserBean]: can not instantiate from JSON object (need to add/enable type information?)
package com.jackson.utils;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
/**
* The class JacksonUtil
*
* json字符与对像转换
*
* @version: $Revision$ $Date$ $LastChangedBy$
*
*/
public final class JacksonUtil {
public static ObjectMapper objectMapper;
/**
* 使用泛型方法,把json字符串转换为相应的JavaBean对象。
* (1)转换为普通JavaBean:readValue(json,Student.class)
* (2)转换为List,如List,将第二个参数传递为Student
* [].class.然后使用Arrays.asList();方法把得到的数组转换为特定类型的List
*
* @param jsonStr
* @param valueType
* @return
*/
public static T readValue(String jsonStr, Class valueType) {
if (objectMapper == null) {
objectMapper = new ObjectMapper();
}
try {
return objectMapper.readValue(jsonStr, valueType);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* json数组转List
* @param jsonStr
* @param valueTypeRef
* @return
*/
public static T readValue(String jsonStr, TypeReference valueTypeRef){
if (objectMapper == null) {
objectMapper = new ObjectMapper();
}
try {
return objectMapper.readValue(jsonStr, valueTypeRef);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 把JavaBean转换为json字符串
*
* @param object
* @return
*/
public static String toJSon(Object object) {
if (objectMapper == null) {
objectMapper = new ObjectMapper();
}
try {
return objectMapper.writeValueAsString(object);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
package com.jackson.main;
import java.util.ArrayList;
import java.util.List;
import com.fasterxml.jackson.core.type.TypeReference;
import com.jackson.bean.DeptBean;
import com.jackson.bean.UserBean;
import com.jackson.utils.JacksonUtil;
/**
* 实例实现利用jackson实现实体对象与json字符串的互相转换
* @author liangming.deng
*
*/
public class JacksonDemo {
public static void main(String[] args){
UserBean userBean1 = new UserBean(1, "liubei", "123", "[email protected]");
UserBean userBean2 = new UserBean(2, "guanyu", "123", "[email protected]");
UserBean userBean3 = new UserBean(3, "zhangfei", "123", "[email protected]");
List userBeans = new ArrayList<>();
userBeans.add(userBean1);
userBeans.add(userBean2);
userBeans.add(userBean3);
DeptBean deptBean = new DeptBean(1, "sanguo", userBeans);
//对象转json
String userBeanToJson = JacksonUtil.toJSon(userBean1);
String deptBeanToJson = JacksonUtil.toJSon(deptBean);
System.out.println("deptBean to json:" + deptBeanToJson);
System.out.println("userBean to json:" + userBeanToJson);
//json转字符串
UserBean jsonToUserBean = JacksonUtil.readValue(userBeanToJson, UserBean.class);
DeptBean jsonToDeptBean = JacksonUtil.readValue(deptBeanToJson, DeptBean.class);
System.out.println("json to DeptBean" + jsonToDeptBean.toString());
System.out.println("json to UserBean" + jsonToUserBean.toString());
//List 转json字符串
String listToJson = JacksonUtil.toJSon(userBeans);
System.out.println("list to json:" + listToJson);
//数组json转 List
List jsonToUserBeans = JacksonUtil.readValue(listToJson, new TypeReference>() {
});
String userBeanString = "";
for (UserBean userBean : jsonToUserBeans) {
userBeanString += userBean.toString() + "\n";
}
System.out.println("json to userBeans:" + userBeanString);
}
}
deptBean to json:{"deptId":1,"deptName":"sanguo","userBeanList":[{"userId":1,"userName":"liubei","password":"123","email":"[email protected]"},{"userId":2,"userName":"guanyu","password":"123","email":"[email protected]"},{"userId":3,"userName":"zhangfei","password":"123","email":"[email protected]"}]}
userBean to json:{"userId":1,"userName":"liubei","password":"123","email":"[email protected]"}
json to DeptBeanDeptBean [deptId=1, deptName=sanguo,
userBeanListString=UserBean [userId=1, userName=liubei, password=123, [email protected]]
UserBean [userId=2, userName=guanyu, password=123, [email protected]]
UserBean [userId=3, userName=zhangfei, password=123, [email protected]]
]
json to UserBeanUserBean [userId=1, userName=liubei, password=123, [email protected]]
list to json:[{"userId":1,"userName":"liubei","password":"123","email":"[email protected]"},{"userId":2,"userName":"guanyu","password":"123","email":"[email protected]"},{"userId":3,"userName":"zhangfei","password":"123","email":"[email protected]"}]
json to userBeans:UserBean [userId=1, userName=liubei, password=123, [email protected]]
UserBean [userId=2, userName=guanyu, password=123, [email protected]]
UserBean [userId=3, userName=zhangfei, password=123, [email protected]]
jackson实现带日期的实体对象与json转换有两种方法
1).将实体对象中的日期对象定义为String型,在使用的时候再将String型转换为Date型使用,其他就无需修改。
2).当实体对象中的日期对象定义为Date型,就需要通过集成JsonSerializer
private Date createDate;
public Date getCreateDate() {
return createDate;
}
public void setCreateDate(Date createDate) {
this.createDate = createDate;
}
package com.jackson.utils;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
/**
* jackson日期转换工具类
*
* @author liangming.deng
*
*/
public class JsonDateFormatFull extends JsonSerializer {
/**
* Jackson支持日期字符串格式
* "yyyy-MM-dd'T'HH:mm:ss.SSSZ" "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"
* "EEE, dd MMM yyyy HH:mm:ss zzz" "yyyy-MM-dd"
*/
@Override
public void serialize(Date value, JsonGenerator jgen,
SerializerProvider provider) throws
IOException, JsonProcessingException {
SimpleDateFormat formatter = new SimpleDateFormat(
"yyyy-MM-dd'T'HH:mm:ss");
String formattedDate = formatter.format(value);
jgen.writeString(formattedDate);
}
}
@JsonSerialize(using = JsonDateFormateFull.class)
private Date createDate;
package com.jackson.main;
import java.util.Date;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import com.fasterxml.jackson.core.type.TypeReference;
import com.jackson.bean.DeptBean;
import com.jackson.bean.UserBean;
import com.jackson.utils.JacksonUtil;
/**
*利用jackson实现Json与实体对象的互转
* @author liangming.deng
*
*/
public class JacksonDemo {
public static void main(String[] args){
UserBean userBean1 = new UserBean(1, "liubei", "123", "[email protected]");
userBean1.setCreateDate(new Date(Calendar.getInstance().getTimeInMillis()));
UserBean userBean2 = new UserBean(2, "guanyu", "123", "[email protected]");
userBean2.setCreateDate(new Date(Calendar.getInstance().getTimeInMillis()));
UserBean userBean3 = new UserBean(3, "zhangfei", "123", "[email protected]");
userBean3.setCreateDate(new Date(Calendar.getInstance().getTimeInMillis()));
List userBeans = new ArrayList<>();
userBeans.add(userBean1);
userBeans.add(userBean2);
userBeans.add(userBean3);
DeptBean deptBean = new DeptBean(1, "sanguo", userBeans);
deptBean.setCreateDate(new Date(Calendar.getInstance().getTimeInMillis()));
//实体对象 转json
String userBeanToJson = JacksonUtil.toJSon(userBean1);
String deptBeanToJson = JacksonUtil.toJSon(deptBean);
System.out.println("deptBean to json:" + deptBeanToJson);
System.out.println("userBean to json:" + userBeanToJson);
//json 转实体对象
UserBean jsonToUserBean = JacksonUtil.readValue(userBeanToJson, UserBean.class);
DeptBean jsonToDeptBean = JacksonUtil.readValue(deptBeanToJson, DeptBean.class);
System.out.println("json to DeptBean" + jsonToDeptBean.toString());
System.out.println("json to UserBean" + jsonToUserBean.toString());
//List 转json
String listToJson = JacksonUtil.toJSon(userBeans);
System.out.println("list to json:" + listToJson);
//json 转 List
List jsonToUserBeans = JacksonUtil.readValue(listToJson, new TypeReference>() {
});
String userBeanString = "";
for (UserBean userBean : jsonToUserBeans) {
userBeanString += userBean.toString() + "\n";
}
System.out.println("json to userBeans:" + userBeanString);
}
}
deptBean to json:{"deptId":1,"deptName":"sanguo","createDate":"2014-11-20T10:58:08","userBeanList":[{"userId":1,"userName":"liubei","password":"123","email":"[email protected]","createDate":"2014-11-20T10:58:08"},{"userId":2,"userName":"guanyu","password":"123","email":"[email protected]","createDate":"2014-11-20T10:58:08"},{"userId":3,"userName":"zhangfei","password":"123","email":"[email protected]","createDate":"2014-11-20T10:58:08"}]}
userBean to json:{"userId":1,"userName":"liubei","password":"123","email":"[email protected]","createDate":"2014-11-20T10:58:08"}
json to DeptBeanDeptBean [deptId=1, deptName=sanguo,
userBeanListString=UserBean [userId=1, userName=liubei, password=123, [email protected], createDate=Thu Nov 20 18:58:08 CST 2014]
UserBean [userId=2, userName=guanyu, password=123, [email protected], createDate=Thu Nov 20 18:58:08 CST 2014]
UserBean [userId=3, userName=zhangfei, password=123, [email protected], createDate=Thu Nov 20 18:58:08 CST 2014]
]
json to UserBeanUserBean [userId=1, userName=liubei, password=123, [email protected], createDate=Thu Nov 20 18:58:08 CST 2014]
list to json:[{"userId":1,"userName":"liubei","password":"123","email":"[email protected]","createDate":"2014-11-20T10:58:08"},{"userId":2,"userName":"guanyu","password":"123","email":"[email protected]","createDate":"2014-11-20T10:58:08"},{"userId":3,"userName":"zhangfei","password":"123","email":"[email protected]","createDate":"2014-11-20T10:58:08"}]
json to userBeans:UserBean [userId=1, userName=liubei, password=123, [email protected], createDate=Thu Nov 20 18:58:08 CST 2014]
UserBean [userId=2, userName=guanyu, password=123, [email protected], createDate=Thu Nov 20 18:58:08 CST 2014]
UserBean [userId=3, userName=zhangfei, password=123, [email protected], createDate=Thu Nov 20 18:58:08 CST 2014]
源码下载地址:http://download.csdn.net/detail/a123demi/8394327