@JsonProperty的使用

时间:2023-03-08 16:55:39

jackson的maven依赖

<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.5.3</version>
</dependency>

aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAV4AAAA7CAIAAAC8B3UQAAAG6ElEQVR4nO2cv5LjRBDG97lUe2Z5hTXGVUQXUBQuu8hINrAdE1xiLZFDrItInDET8gKXE1Ek8BYEskY9Mz3jkfVnRtJH/QLjlazuVvtTz+ze9/T81dcAAGDwFD0CAECCOKXhn18/lXz5+Bo9SgDAwKQoDdlivS+ElPK0fYleoCqk3Unkm0UdT7Y6XkSxX4VGWCaVTkZBMSefY7bNRXFcLh68oivgRom3jCFZbtJwPp/P5/Pb25t6EVEalodC5LvopaFAGtLMMVlpyFbHixBSSvYJt8lvP5JSMucudichpJRCb7mBcUrDf79/Lvnr5+8HjmmTi8th3f4Gd4gtDc0/YXzSkH6Ofmm4G08nAdsxZIvdqXonWx0vVud4OjxbrPfF7fhNLiI+I4Omhk9//l3y3S+XAWKCNIwUSIPjEuYAEtjhcZcqN2m4Xq/X6/X9/V29oNLw+cu/JT/99oeV+W34kVKW2dJ31ESULXYnUey3tynrtH3Jtjk9i1aNnssOZstDoY758cO2vlxxXC5euJDKDtidhBDF8YdDoQIrhflueI4FRZUdF6QRBu3C8vjygUBzIbVyFPDAF63JjdBKwZerGqdTy9G+KB3IjQi1S5SZWinQamjvV4m7QmJjYJch7EPFaHLXl7PpM7IXaXhsaigLRKM33sm2uahLXPXiNpdSlk3jH7eyxXqfVzdefZR1itaR5b1c0Rv/Ur6gt6G8hFLlu+F5pIEPkqlMFcw2FypCJhdvAeuotBZsciO0UvDlsqQhhRzpuUqbNrkSODZC2hjOA+pqWD3GhuSMgYvZ//VeHgrPPkXcGbOdNNirLP0d8uWkLci/dlXTeObc7hY5S+uA6mmv/jOeZrTDxL2Q1Gv/1MAEyaw/1/tCnPKcKhSTS1ABzRm1yY3QSsGWK6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YmZo0wDY2EeaQ47Tp/Q+lB84HtrGJMIcc9WTbG7em9fejvUgDbGP1S8Abcso5Zm2NWzU/wejpKKb2z6sgDSNlGjk2NW7VzvVa4wxPVL8G2MY6ZtGBLVWDb8T4bWN7yNT6/AbGrcZhjZ5qfRNtaoBtbCKWqnOyje0lU7b9WNh/qK50M7WJKZ40wDYWtrHD2sb2lCntvabGrebpE1tQwDYWtrE2SeXYa6aq8R4wbg3/6fBEXlDANha2scPZxvaUaRvj1tXxQjxKpzY1wDYWtrFyDLaxfWXazrjV3mdNhGhTw5yZg6XqHHKcNpCGCKQ2OiJHYNOBNIBw5mCpOocc5wCkAQDA8D9hvj4MuPSNCgAAAABJRU5ErkJggg==" alt="" />所以引入这一个依赖就可以了

@JsonProperty 此注解用于属性上,作用是把该属性的名称序列化为另外一个名称,如把trueName属性序列化为name,@JsonProperty(value="name")。

import com.fasterxml.jackson.annotation.JsonProperty;

public class Student {

    @JsonProperty(value = "real_name")
private String realName; public String getRealName() {
return realName;
} public void setRealName(String realName) {
this.realName = realName;
} @Override
public String toString() {
return "Student{" +
"realName='" + realName + '\'' +
'}';
}
}

测试

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper; public class Main {
public static void main(String[] args) throws JsonProcessingException {
Student student = new Student();
student.setRealName("zhangsan");
System.out.println(new ObjectMapper().writeValueAsString(student));
}
}

结果

{"real_name":"zhangsan"}

这里需要注意的是将对象转换成json字符串使用的方法是fasterxml.jackson提供的!!

如果使用fastjson呢?

<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.28</version>
</dependency>
import com.alibaba.fastjson.JSON;

public class Main {
public static void main(String[] args) {
Student student = new Student();
student.setRealName("zhangsan");
System.out.println(JSON.toJSONString(student));
}
}

结果

{"realName":"zhangsan"}

可以看到,@JsonProperty(value = "real_name")没有生效,为啥?

因为fastjson不认识@JsonProperty注解呀!所以要使用jackson自己的序列化工具方法!

--------------------------

@JsonProperty不仅仅是在序列化的时候有用,反序列化的时候也有用,比如有些接口返回的是json字符串,命名又不是标准的驼峰形式,在映射成对象的时候,将类的属性上加上@JsonProperty注解,里面写上返回的json串对应的名字
import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.IOException;

public class Main {
public static void main(String[] args) throws IOException {
String jsonStr = "{\"real_name\":\"zhangsan\"}";
Student student = new ObjectMapper().readValue(jsonStr.getBytes(), Student.class);
System.out.println(student);
}
}

结果:

Student{realName='zhangsan'}