如何在 Java 中使用 Jackson 在序列化期間忽略一個類?
Jackson @JsonIgnoreType 註釋 可以用來在 **序列化 過程中 **忽略一個 **類 ,它可以標記類的所有 **屬性 或 **欄位 **在序列化和反序列化 JSON 物件時會被忽略。
語法
@Target(value={ANNOTATION_TYPE,TYPE})
@Retention(value=RUNTIME)
public @interface JsonIgnoreType示例
import com.fasterxml.jackson.annotation.*;
import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.databind.*;
import java.io.*;
public class JsonIgnoreTypeTest {
public static void main(String args[]) throws IOException {
Employee emp = new Employee();
ObjectMapper mapper = new ObjectMapper();
String jsonString = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(emp);
System.out.println(jsonString);
}
}
// Employee class
class Employee {
@JsonIgnoreType
public static class Address {
public String firstLine = null;
public String secondLine= null;
public String thirdLine = null;
@Override
public String toString() {
return "Address{" +
"firstLine='" + firstLine+ '\'' +
", secondLine='" + secondLine+ '\'' +
", thirdLine='" + thirdLine + '\'' +
'}';
}
} // end of Address class
public long empId = 115;
public String empName = "Raja Ramesh";
public Address empAddress = new Address();
@Override
public String toString() {
return "Employee{" +
"empId=" + empId +
", empName='" + empName + '\'' +
", empAddress=" + empAddress +
'}';
}
}輸出
{
"empId" : 115,
"empName" : "Raja Ramesh"
}
廣告
資料結構
網路
關係型資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C 語言
C++
C#
MongoDB
MySQL
JavaScript
PHP