
如果Java对象中存在不希望被序列化的字段,我们可以使用@JsonIgnore注解在Jackson库中。在序列化和反序列化期间,@JsonIgnore可以用于忽略字段。
Easily find JSON paths within JSON objects using our intuitive Json Path Finder
30
public @interface JsonIgnore
import java.io.*;
import java.util.*;
import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.annotation.*;
public class JsonIgnoreAnnotationTest {
public static void main(String args[]) throws JsonGenerationException, JsonMappingException, IOException {
Employee emp = new Employee();
emp.setFirstName("Raja");
emp.setLastName("Ramesh");
emp.setEmpId(120);
emp.getTechnologies().add("Java");
emp.getTechnologies().add("Scala");
emp.getTechnologies().add("Python");
ObjectMapper mapper = new ObjectMapper();
mapper.writerWithDefaultPrettyPrinter().writeValue(System.out, emp);
}
}
// Employee class
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
"firstName",
"lastName",
"technologies",
"empId"
})
class Employee {
<strong> </strong>@JsonProperty("EMPLOYEE_ID")
private int empId;
@JsonProperty("EMPLOYEE_FIRST_NAME")
private String firstName;
@JsonProperty("EMPLOYEE_LAST_NAME")
private String lastName;
@JsonIgnore <strong>
</strong> private List<String> technologies = new ArrayList<>();
public int getEmpId() {
return empId;
}
public void setEmpId(int empId) {
this.empId = empId;
}
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 List<String> getTechnologies() {
return technologies;
}
public void setTechnologies(List<String> technologies) {
this.technologies = technologies;
}
}{
"EMPLOYEE_FIRST_NAME" : "Raja",
"EMPLOYEE_LAST_NAME" : "Ramesh",
"EMPLOYEE_ID" : 120
}以上就是在Java中,我们如何在JSON序列化过程中忽略某些字段?的详细内容,更多请关注php中文网其它相关文章!
java怎么学习?java怎么入门?java在哪学?java怎么学才快?不用担心,这里为大家提供了java速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号