package com.yc.model; /**
* 班级
* @author Administrator
*
*/
public class ClassModel { private String classCode;
private String className; public String getClassCode() {
return classCode;
} public void setClassCode(String classCode) {
this.classCode = classCode;
} public String getClassName() {
return className;
} public void setClassName(String className) {
this.className = className;
} }
package com.yc.model; import java.util.Date; /**
* 学生表
* @author WUYF
*
*/
public class StudentModel {
private String classCode;
private String stuCode;
private String stuName;
private Integer stuAge;
private Date stuBirthday = new Date(); public String getClassCode() {
return classCode;
} public void setClassCode(String classCode) {
this.classCode = classCode;
} public String getStuCode() {
return stuCode;
} public void setStuCode(String stuCode) {
this.stuCode = stuCode;
} public String getStuName() {
return stuName;
} public void setStuName(String stuName) {
this.stuName = stuName;
} public Integer getStuAge() {
return stuAge;
} public void setStuAge(Integer stuAge) {
this.stuAge = stuAge;
} public Date getStuBirthday() {
return stuBirthday;
} public void setStuBirthday(Date stuBirthday) {
this.stuBirthday = stuBirthday;
} }
package com.yc.model; import java.util.Set; /**
* 班级信息
* @author Administrator
*
*/
public class ClassInfo { private ClassModel classModel;
private Set<StudentModel> studentModelList; public ClassModel getClassModel() {
return classModel;
} public void setClassModel(ClassModel classModel) {
this.classModel = classModel;
} public Set<StudentModel> getStudentModelList() {
return studentModelList;
} public void setStudentModelList(Set<StudentModel> studentModelList) {
this.studentModelList = studentModelList;
} }
package com.yc.test; import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set; import javax.naming.InitialContext; import com.fasterxml.jackson.core.JsonEncoding;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.yc.model.ClassInfo;
import com.yc.model.ClassModel;
import com.yc.model.StudentModel; @SuppressWarnings("unused")
public class jacksonTest { /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
init();
doTest();
testJsonGenerate();
} /******************** 变量定义 ************************/
private static ObjectMapper jsonMapper = new ObjectMapper();
private static ClassInfo classInfo = new ClassInfo(); private static JsonGenerator jsonGenerator = null; /******************** 初始化 **************************/
private static void init()
{
classInfo.setStudentModelList(new HashSet<StudentModel>());
// 班级数据初骀化
ClassModel classModel = new ClassModel();
classModel.setClassCode("001");
classModel.setClassName("计0312班"); classInfo.setClassModel(classModel); // 学生数据初始化
for(Integer i= 1; i<5;i++)
{
StudentModel studentModel = new StudentModel();
studentModel.setClassCode(classModel.getClassCode());
studentModel.setStuCode("00" + i.toString());
studentModel.setStuName("学生_" + studentModel.getStuCode());
studentModel.setStuAge(18 + i); classInfo.getStudentModelList().add(studentModel);
} System.out.println(classInfo); } /******************** 测试主体 **************************/ private static void testJsonGenerate()
{
try {
jsonGenerator = jsonMapper.getFactory().createGenerator(System.out,JsonEncoding.UTF8); jsonGenerator.writeObject(classInfo); jsonGenerator.flush();
jsonGenerator.close(); } catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} } /**
* @throws IOException
*
*/
private static void doTest()
{
try {
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); jsonMapper.setDateFormat(dateFormat); ClassInfo classInfoTmp = new ClassInfo(); String classInfoJsonString = jsonMapper.writeValueAsString(classInfo);
System.out.println("classInfoJsonString:");
System.out.println(classInfoJsonString); classInfoTmp = jsonMapper.readValue(classInfoJsonString, ClassInfo.class); String classInfoJsonTmpString = jsonMapper.writeValueAsString(classInfoTmp);
System.out.println("classInfoJsonTmpString:");
System.out.println(classInfoJsonTmpString); } catch (JsonProcessingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} }
另外 可以在字段属性上增加 注解来实现自定义日期格式
使用Jackson时转换JSON时,日期格式设置
近日,使用Jackson转化JSON对象的时候,显示的时候,日期始终显示不正确,输出的日期是一串数字代表的时间戳,不符合要求,所以想到Jackson应当有方法设置输出的日期格式。后来一查果然有两种方式来实现:
1.普通的方式:
默认是转成timestamps形式的,通过下面方式可以取消timestamps。
objectMapper.configure(SerializationConfig.Feature.WRITE_DATES_AS_TIMESTAMPS, false);
这样将使时间生成使用所谓的use a [ISO-8601 ]-compliant notation, 输出类似如下格式的时间: "1970-01-01T00:00:00.000+0000".
当然也可以自定义输出格式:
objectMapper.getSerializationConfig().setDateFormat(myDateFormat);
myDateFormat对象为java.text.DateFormat,具体使用清查java API
2.annotaion的注释方式:
先定义自己需要的格式,例如:
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.codehaus.jackson.JsonGenerator;
import org.codehaus.jackson.JsonProcessingException;
import org.codehaus.jackson.map.JsonSerializer;
import org.codehaus.jackson.map.SerializerProvider; /**
* java日期对象经过Jackson库转换成JSON日期格式化自定义类
* @author godfox
* @date 2010-5-3
*/
public class CustomDateSerializer extends JsonSerializer<Date> { @Override
public void serialize(Date value, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonProcessingException {
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
String formattedDate = formatter.format(value);
jgen.writeString(formattedDate);
}
}
然后在你的POJO上找到日期的get方法
@JsonSerialize(using = CustomDateSerializer.class)
public Date getCreateAt() {
return createAt;
}
// 反序列化 成 javabean
ObjectMapper jsonMapper = new ObjectMapper();
rtnResult = jsonMapper.writeValueAsString(invoiceDtos); List<InvoiceDetail> deJson = (List<InvoiceDetail>)jsonMapper.readValue(rtnResult, new TypeReference<List<InvoiceDetail>>() {});