
Easily find JSON paths within JSON objects using our intuitive Json Path Finder
30
@JsonIgnorePropertiesJackson 注释可用于指定要忽略的类的属性列表或字段。 @JsonIgnoreProperties annotation 可以放置在类声明之前,而不是放置在单个属性或字段之前以进行忽略。
@Target(value={ANNOTATION_TYPE,TYPE,METHOD,CONSTRUCTOR,FIELD})
@Retention(value=RUNTIME)
public @interface JsonIgnorePropertiesimport java.io.*;
import com.fasterxml.jackson.annotation.*;
import com.fasterxml.jackson.databind.*;
public class JsonIgnorePropertiesTest {
public static void main(String[] args) throws IOException {
Customer customer = new Customer("120", "Ravi", "Hyderabad");
System.out.println(customer);
ObjectMapper mapper = new ObjectMapper();
String jsonString = mapper.writeValueAsString(customer);
System.out.println("JSON: " + jsonString);
System.out.println("---------");
jsonString = "{\"id\":\"130\",\"name\":\"Rahul\", \"address\":\"Mumbai\"}";
System.out.println("JSON: " + jsonString);
customer = mapper.readValue(jsonString, Customer.class);
System.out.println(customer);
}
}
// Customer class
@JsonIgnoreProperties({"id", "address"}) <strong>
</strong>class Customer {
private String id;
private String name;
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;
}
<strong> </strong>@Override
public String toString() {
return "Customer [id=" + id + ", name=" + name + ", address=" + address + "]";
}
}Customer [id=120, name=Ravi, address=Hyderabad]
JSON: {"name":"Ravi"}
---------
JSON: {"id":"130","name":"Rahul", "address":"Mumbai"}
Customer [id=null, name=Rahul, address=null]以上就是如何在Java中忽略JSON对象的多个属性?的详细内容,更多请关注php中文网其它相关文章!
java怎么学习?java怎么入门?java在哪学?java怎么学才快?不用担心,这里为大家提供了java速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号