jackson的maven依赖
- <dependency>
- <groupId>com.fasterxml.jackson.core</groupId>
- <artifactId>jackson-databind</artifactId>
- <version>2.5.3</version>
- </dependency>
- import com.fasterxml.jackson.annotation.JsonProperty;
- public class Student {
- @JsonProperty("name")
- private String trueName;
- public String getTrueName() {
- return trueName;
- }
- public void setTrueName(String trueName) {
- this.trueName = trueName;
- }
- }
- 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.setTrueName("张三");
- System.out.println(new ObjectMapper().writeValueAsString(student));
- }
- }
- {"name":"张三"}
@JsonProperty不仅仅是在序列化的时候有用,反序列化的时候也有用,比如有些接口返回的是json字符串,命名又不是标准的驼峰形式,在映射成对象的时候,将类的属性上加上@JsonProperty注解,里面写上返回的json串对应的名字
jackson的jar包点我免费下载
转载。 https://blog.youkuaiyun.com/Paranoia_ZK/article/details/69388622
本文介绍如何使用Jackson库进行JSON数据的序列化与反序列化。通过示例代码展示了如何定义类来处理不同名称的属性,以及如何将Java对象转换为JSON字符串。
1万+

被折叠的 条评论
为什么被折叠?



