
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
How to convert bean to JSON object by excluding some properties using JsonConfig in Java?
The JsonConfig class is a utility class that helps to configure the serialization process. We can convert a bean to a JSON object with few properties that can be excluded using the setExcludes() method of JsonConfig class and pass this JSON config instance to an argument of static method fromObject() of JSONObject.
Syntax
public void setExcludes(String[] excludes)
In the below example, we can convert bean to a JSON object by excluding some of the properties.
Example
import net.sf.json.JSONObject; import net.sf.json.JsonConfig; public class BeanToJsonExcludeTest { public static void main(String[] args) { Student student = new Student("Raja", "Ramesh", 35, "Madhapur"); JsonConfig jsonConfig = new JsonConfig(); jsonConfig.setExcludes(new String[]{"age", "address"}); JSONObject obj = JSONObject.fromObject(student, jsonConfig); System.out.println(obj.toString(3)); //pretty print JSON } public static class Student { private String firstName, lastName, address; private 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; } } }
In the below output, age and address properties can be excluded.
Output
{ "firstName": "Raja", "lastName": "Ramesh" }
- Related Articles
- How to convert the JSON object to a bean using JSON-lib API in Java?
- How to convert a bean to JSON object using Exclude Filter in Java?
- How to convert a JSON string to a bean using JSON-lib API in Java?
- How to convert a bean to XML using JSON-lib API in Java?
- How to convert a bean to XML without type hints using JSON-lib API in Java?
- How to convert Java object to JSON using GSON library?
- How to convert Java object to JSON using Jackson library?
- How to convert a Map to JSON object using JSON-lib API in Java?
- Convert JSON object to Java object using Gson library in Java?\n
- How to convert a JSON object to an enum using Jackson in Java?
- How to convert a JSON to Java Object using the Jackson library in Java?\n
- Convert a JSON String to Java Object using the json-simple library in Java?\n
- Convert Java object to JSON using the Gson library in Java?\n
- How can we convert a JSON string to a JSON object in Java?
- How to convert JSON object to Hashtable format using PowerShell?

Advertisements