jackson简单用法
Name类:
public class Name {
private String firstName;
private String lastName;
public Name(){}
public Name(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String toString() {
return firstName + " " + lastName;
}
}
Student类:
import java.util.Date;
public class Student {
private int id;
private Name name;
private String className;
private Date birthDay;
public Student(){}
public Student(int id, Name name, String className, Date birthDay) {
super();
this.id = id;
this.name = name;
this.className = className;
this.birthDay = birthDay;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public Name getName() {
return name;
}
public void setName(Name name) {
this.name = name;
}
public Date getBirthDay() {
return birthDay;
}
public void setBirthDay(Date birthDay) {
this.birthDay = birthDay;
}
public String getClassName() {
return className;
}
public void setClassName(String className) {
this.className = className;
}
@Override
public String toString() {
return "Student [birthDay=" + birthDay + ", id=" + id + ", name=" + name + ", classname="+ className + "]";
}
}
测试类
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.codehaus.jackson.JsonGenerator;
import org.codehaus.jackson.map.DeserializationConfig;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.SerializationConfig;
import org.codehaus.jackson.map.annotate.JsonSerialize.Inclusion;
import org.codehaus.jackson.type.TypeReference;
public class JacksonTest {
public static ObjectMapper getDefaultObjectMapper() {
ObjectMapper mapper = new ObjectMapper();
//设置将对象转换成JSON字符串时候:包含的属性不能为空或"";
//Include.Include.ALWAYS 默认
//Include.NON_DEFAULT 属性为默认值不序列化
//Include.NON_EMPTY 属性为 空("") 或者为 NULL 都不序列化
//Include.NON_NULL 属性为NULL 不序列化
mapper.setSerializationInclusion(Inclusion.NON_EMPTY);
//设置将MAP转换为JSON时候只转换值不等于NULL的
mapper.configure(SerializationConfig.Feature.WRITE_NULL_MAP_VALUES, false);
mapper.configure(JsonGenerator.Feature.ESCAPE_NON_ASCII, true);
mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
//设置有属性不能映射成PO时不报错
mapper.disable(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES);
return mapper;
}
public static void main(String[] args) throws Exception{
//准备数据
Name name1 = new Name("zhang","san");
Name name2 = new Name("li","si");
Name name3 = new Name("wang","wu");
Student student1 = new Student(1,name1,"一班",new Date());
Student student2 = new Student(2,name2,"二班",new Date());
Student student3 = new Student(3,name3,"三班",new Date());
List<Student> studentList = new ArrayList<Student>();
studentList.add(student1);
studentList.add(student2);
studentList.add(student3);
Map<String,Student> studentMap = new HashMap<String, Student>();
studentMap.put("1", student1);
studentMap.put("2", student2);
studentMap.put("3", student3);
Student json2object = null;
List<Student> json2list = null;
Map<String,Student> json2map = null;
ObjectMapper mapper = getDefaultObjectMapper();
/* Object --> JSON */
String object4json = mapper.writeValueAsString(student1);
System.out.println("Object ----> JSON");
System.out.println(object4json);
System.out.println("------------------------------------------------------");
/* List<Object> --> JSON */
String listforjson = mapper.writeValueAsString(studentList);
System.out.println("List<Object> ----> JSON");
System.out.println(listforjson);
System.out.println("------------------------------------------------------");
/* Map<String,Object> ----> JSON */
String map4json = mapper.writeValueAsString(studentMap);
System.out.println("Map<String,Object> ----> JSON");
System.out.println(map4json);
System.out.println("------------------------------------------------------");
/* JSON --> Object */
json2object = mapper.readValue(object4json, Student.class);
System.out.println("JSON ----> Object");
System.out.println(json2object);
System.out.println("------------------------------------------------------");
/* JSON --> List<Object> */
json2list = mapper.readValue(listforjson, new TypeReference<List<Student>>() {});
System.out.println("JSON --> List<Object>");
System.out.println(json2list.toString());
System.out.println("------------------------------------------------------");
/* JSON --> Map<String,Object> */
json2map = mapper.readValue(map4json, new TypeReference<Map<String,Student>>() {});
System.out.println("JSON --> Map<String,Object>");
System.out.println(json2map.toString());
}
}