// pom依赖
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.8</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.8</version>
</dependency>
Jackson的readValue以及write的使用
1.定义一个User对象
public class User {
private String name;
private int age;
private Date birthday; // 没有使用jackson注解将会变成时间戳
private String email;
public User(String name, int age, Date birthday, String email) {
this.name = name;
this.age = age;
this.birthday = birthday;
this.email = email;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
2.使用jackson 的ObjectMapper 类中的 readValue()方法进行转换
public class JsonTest
{
public static void main(String[] args) throws JsonGenerationException, JsonMappingException, IOException {
User user=new User("隔壁老王", 18, new Date(), "110");
User user1=new User("隔壁老王", 18, new Date(), "110");
List<User> list = new ArrayList<User>();
list.add(user);
list.add(user1);
//转换器
ObjectMapper mapper = new ObjectMapper();
// 对象--json数据
String json=mapper.writeValueAsString(user); //将对象转换成json
// 将json 字符串转成User类
User u = mapper.readValue(json, User.class);
// 将json 字符串转化成List<User>
List<User> list1 = mapper.readValue(list, new TypeReference<List<People>>(){}f)
// 其他的如将文件中转化成类的方法也相识 如
// readValue(File src, TypeReference valueTypeRef)
// readValue(File src, Class<T> valueType)
}
}
3.使用jackson 的ObjectMapper 类中的 writerValue()方法进行转换
public class JsonTest
{
public static void main(String[] args) throws JsonGenerationException, JsonMappingException, IOException {
SimpleDateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd");
User user=new User("隔壁老王", 18, dateformat.parse("1996-10-01"), "110");
//转换器
ObjectMapper mapper = new ObjectMapper();
//User类转JSON 字符串
String json = mapper.writeValueAsString(user);
System.out.println(json);
//Java集合转JSON 字符串
List<User> users = new ArrayList<User>();
users.add(user);
String jsonlist = mapper.writeValueAsString(users);
System.out.println(jsonlist);
}
}
}
4.使用jackson ObjectMapper获取具体json 字符串中某个key值对于的value
// 假设 json字符串格式为 {content:{rendered: "23"}, titile:{rendered: "别跑"}} 则获取方式如下:
ObjectMapper mapper = new ObjectMapper();
mapper.readTree(content).forEach(jsonNode -> {
String renderedContent = jsonNode.path("content").path("rendered").asText("");
String title = jsonNode.path("title").path("rendered").asText("");
});
5.使用jackson 的提供了一系列注解
@JsonIgnore 此注解用于属性上,作用是进行JSON操作时忽略该属性。
@JsonFormat 此注解用于属性上,作用是把Date类型直接转化为想要的格式,如@JsonFormat(pattern = "yyyy-MM-dd HH-mm-ss")。
@JsonProperty 此注解用于属性上,作用是把该属性的名称序列化为另外一个名称,如把trueName属性序列化
链接:https://www.jianshu.com/p/7e4bc9a535c8
Jackson常用注解及用法
最近写项目,用到Jackson的一些注解,总结一下,帮助自己记忆。
1.@JsonIgnore 和 @JsonIgnoreProperties
两个注解可以对照比较后选择使用:
@JsonIgnore
在json序列化时将java bean中的一些属性忽略掉,序列化和反序列化都受影响。
private String name;
private Integer age;
@JsonIgnore
private String color;
运行方法在控制台打印
System.out.println(name+""+age+""+color+"");
将只显示name和age,color受注解的影响不进行显示。
使用方法:一般标记在属性或者方法上,返回的json数据即不包含该属性。
@JsonIgnoreProperties
和 @JsonIgnore 的作用相同,都是告诉 Jackson 该忽略哪些属性,
不同之处是 @JsonIgnoreProperties 是类级别的,并且可以同时指定多个属性。
@JsonIgnoreProperties(value = {"age","color"})
public class TestJackson{
private String id;
private String username;
private String password;
private Integer age;
private String color;
}
类中的age和color属性都将会在序列化和反序列化时被忽略掉。
2.@JsonProperty
它用于属性上,作用是把属性的名称序列化成另外一个名称
@JsonProperty("name")
private String username;
把username属性序列化为name
3.@JsonInclude(JsonInclude.Include.NON_NULL)
这个注解表示,如果值为null,则不返回,还可以在类上添加这个注释,当实体类与json互相转换的时候,属性值为null的不参与序列化。
@JsonInclude(JsonInclude.Include.NON_NULL)
public Vector children;
@JsonInclude(JsonInclude.Include.NON_NULL)
public class TestJackson{
xxxxxx