Json 数据处理技术 之 Jackson
Maven依赖
com.fasterxml.jackson.core
jackson-core
2.9.7
com.fasterxml.jackson.core
jackson-databind
2.9.7
com.fasterxml.jackson.core
jackson-annotations
2.9.7
示例:实体类序列化和反序列化
public static void main(String[] args) throws IOException {
ObjectMapper objectMapper = new ObjectMapper();
Dept dept = new Dept(10, "ACCOUNTING", "NEWYORK");
//序列化
String json = objectMapper.writeValueAsString(dept);
System.out.println(json);
//反序列化
Dept res = objectMapper.readValue(json, Dept.class);
System.out.println(res);
}
示例:List序列化和反序列化
public static void main(String[] args) throws IOException {
ObjectMapper objectMapper = new ObjectMapper();
List depts = new ArrayList();
depts.add(new Dept(10,"ACCOUNTING","NEWYORK"));
depts.add(new Dept(20,"RESEARCH","DALLAS"));
depts.add(new Dept(30,"SALES","CHICAGO"));
depts.add(new Dept(40,"OPERATIONS","BOSTON"));
//序列化
String json = objectMapper.writeValueAsString(depts);
System.out.println(json);
//反序列化
JavaType type = objectMapper.getTypeFactory()
.constructParametricType(ArrayList.class, Dept.class);
List res = objectMapper.readValue(json, type);
res.forEach(System.out::println);
}
示例:Map序列化和反序列化
public static void main(String[] args) throws IOException {
ObjectMapper objectMapper = new ObjectMapper();
Map map = new HashMap();
map.put("ACCOUNTING",new Dept(10,"ACCOUNTING","CHICAGO"));
map.put("RESEARCH",new Dept(20,"RESEARCH","DALLAS"));
map.put("SALES",new Dept(30,"SALES","CHICAGO"));
map.put("OPERATIONS",new Dept(40,"OPERATIONS","BOSTON"));
//序列化
String json = objectMapper.writeValueAsString(map);
System.out.println(json);
//反序列化
JavaType type = objectMapper.getTypeFactory()
.constructParametricType(HashMap.class, String.class,Dept.class);
Map res = objectMapper.readValue(json, type);
res.forEach((k,v)-> System.out.println(k+" : "+v));
}
注解
- JsonIgnore:用于属性上,作用是进行JSON操作时忽略该属性。
- JsonFormat:用于属性上,作用是把Date类型序列化为指定的格式
- JsonProperty:用于属性上,作用是把属性的名称序列化为另外一个名称
- JsonUnwrapped:用于属性上,定义了在序列化/反序列化时应该解包/展开的值,即:把成员对象中的属性提升到其容器类,同时可以添加给定的前缀。
- @JsonSerialize 此注解用于属性上,作用是指定属性序列化的类型。
- @JsonDeserialize 此注解用于属性上,作用是指定属性反序列化的类型。
- @JsonInclude 属性值为null的不参与序列化。
- @JsonInclude(Include.NON_NULL)属性为null,则不参与序列化。
- @JsonPropertyOrder(value={“pname1”,“pname2”}) 改变子属性在JSON序列化中的默认定义的顺序。如:param1在先,param2在后。
- 实体类:
public class Stu {
@JsonProperty("num")
private int id;
private String name;
@JsonFormat(pattern = "yyyy-MM-dd")
private Date birth;
@JsonIgnore
private float score;
@JsonUnwrapped(prefix = "addr_")
private Addr addr;
public Stu() {
}
public Stu(int id, String name, Date birth, float score) {
this.id = id;
this.name = name;
this.birth = birth;
this.score = score;
}
public Stu(int id, String name, Date birth, float score, Addr addr) {
this.id = id;
this.name = name;
this.birth = birth;
this.score = score;
this.addr = addr;
}
public Addr getAddr() {
return addr;
}
public void setAddr(Addr addr) {
this.addr = addr;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getBirth() {
return birth;
}
public void setBirth(Date birth) {
this.birth = birth;
}
public float getScore() {
return score;
}
public void setScore(float score) {
this.score = score;
}
@Override
public String toString() {
return "Stu{" +
"id=" + id +
", name='" + name + '\'' +
", birth=" + birth +
", score=" + score +
'}';
}
public static class Addr {
private String province;
private String city;
public Addr() {
}
public Addr(String province, String city) {
this.province = province;
this.city = city;
}
public String getProvince() {
return province;
}
public void setProvince(String province) {
this.province = province;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
@Override
public String toString() {
return "Addr{" +
"province='" + province + '\'' +
", city='" + city + '\'' +
'}';
}
}
}
- 测试代码:
public static void main(String[] args) throws IOException {
ObjectMapper objectMapper = new ObjectMapper();
Stu.Addr addr = new Stu.Addr("guizhou", "anshun");
Stu stu = new Stu(111, "zhangsan", new Date(), 88.8f,addr);
String json = objectMapper.writeValueAsString(stu);
System.out.println(json);
}