Jackson @JsonAbaikan anotasi boleh digunakan untuk mengabaikan harta atau medan objek Java. Sifat ini boleh diabaikan apabila membaca JSON ke dalam objek Java dan semasa menulis objek Java ke dalam JSON. Kita boleh membaca JSON sebagai objek Java dan menulis objek Java ke JSON menggunakan kaedah readValue() dan writeValueAsString() kelas ObjectMapper.
@Target(value={ANNOTATION_TYPE,METHOD,CONSTRUCTOR,FIELD}) @Retention(value=RUNTIME) public @interface JsonIgnore
import java.io.*; import com.fasterxml.jackson.annotation.*; import com.fasterxml.jackson.databind.*; public class JsonIgnoreTest { public static void main(String[] args) throws IOException { Customer customer = new Customer("110", "Surya Kiran", "Chennai"); System.out.println(customer); ObjectMapper mapper = new ObjectMapper(); String jsonString = mapper.writeValueAsString(customer); System.out.println("JSON: " + jsonString); System.out.println("---------"); jsonString = "{\"id\":\"120\",\"name\":\"Devaraj\", \"address\":\"Banglore\"}"; System.out.println("JSON: " + jsonString); customer = mapper.readValue(jsonString, Customer.class); System.out.println(customer); } } // Customer class<strong> </strong>class Customer { private String id; private String name; <strong> </strong> @JsonIgnore<strong> </strong> private String address; public Customer() { } public Customer(String id, String name, String address) { this.id = id; this.name = name; this.address = address; } public String getId() { return id; } public String getName() { return name; } public String getAddress() { return address; } @Override public String toString() { return "Customer [id=" + id + ", name=" + name + ", address=" + address + "]"; } }
Customer [id=110, name=Surya Kiran, address=Chennai] JSON: {"id":"110","name":"Surya Kiran"} --------- JSON: {"id":"120","name":"Devaraj", "address":"Banglore"} Customer [id=120, name=Devaraj, address=null]
Atas ialah kandungan terperinci Bagaimana untuk mengabaikan medan objek JSON menggunakan perpustakaan Jackson di Jawa?. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!