Home > Java > javaTutorial > How to convert a bean to JSON object using exclude filter in Java?

How to convert a bean to JSON object using exclude filter in Java?

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2023-08-18 19:05:21
forward
1366 people have browsed it

How to convert a bean to JSON object using exclude filter in Java?

You can use the JsonConfig class to configure the serialization process. We can use the setJsonPropertyFilter() method of JsonConfig to set the property filter when serializing to JSON. We need to implement a custom PropertyFilter class by overriding the apply() method of the PropertyFilter interface. Returns true if the attribute will be filtered out, false otherwise.

Syntax

public void setJsonPropertyFilter(PropertyFilter jsonPropertyFilter)
Copy after login

Example

import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import net.sf.json.util.PropertyFilter;
public class ConvertBeanToJsonExcludeFilterTest {
   public static void main(String[] args) {
      Student student = new Student("Sai", "Chaitanya", 20, "Hyderabad");
      JsonConfig jsonConfig = new JsonConfig();
      jsonConfig.setJsonPropertyFilter(new CustomPropertyFilter());

      JSONObject jsonObj = JSONObject.fromObject(student, jsonConfig);
      System.out.println(jsonObj.toString(3)); //pretty print JSON
   }
   public static class Student {
      private String firstName, lastName, address;
      public int age;
      public Student(String firstName, String lastName, int age, String address) {
         super();
         this.firstName = firstName;
         this.lastName = lastName;
         this.age = age;
         this.address = address;
      }
      public String getFirstName() {
         return firstName;
      }
      public String getLastName() {
         return lastName;
      }
      public int getAge() {
         return age;
      }
      public String getAddress() {
         return address;
      }
   }
}
// CustomPropertyFilter class
class CustomPropertyFilter implements PropertyFilter {
   @Override
   public boolean apply(Object source, String name, Object value) {
      if(Number.class.isAssignableFrom(value.getClass()) ||        String.class.isAssignableFrom(value.getClass())) {
         return false;
      }
      return true;
   }
}
Copy after login

Output

{
  "firstName": "Sai",
  "lastName": "Chaitanya",
  "address": "Hyderabad",
  "age": 20
}
Copy after login

The above is the detailed content of How to convert a bean to JSON object using exclude filter in Java?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
Install JAVA
From 1970-01-01 08:00:00
0
0
0
Unable to install java
From 1970-01-01 08:00:00
0
0
0
Can java be used as the backend of the web?
From 1970-01-01 08:00:00
0
0
0
Is this in Java language?
From 1970-01-01 08:00:00
0
0
0
Help: JAVA encrypted data PHP decryption
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template