Convert Java object to JSONJSON stands for JavaScript Object Notation that is a very small weighted format to store and transport data. The JSON format is very easy to understand because it has a self-describing nature. JSON is mainly used to transmit data between a server and a web application. The details of an employee written in the JSON format looks like: Java object is an instance of a class or a self-contained component having properties and methods for making a certain type of data useful. Java object has a state, behavior and identity. Java object of an employee is written in the following way: Many times we need to convert Java objects into JSON so that we can easily understand them. To convert a Java object into a JSON object, we have the following two methods or way:
Let's understand both of them one by one. Using GSON libraryThe GSON library plays an important role in converting a Java object into JSON. GSON library not only converts Java object into JSON but also uses it to convert the JSON string into its equivalent Java object. These are the following steps to convert a Java object into JSON:
Step 1: Create a Maven project In the first step, we need to create a maven project using eclipse IDE. Converting Java objects into JSON is an advanced Java concept, so we recommend you to use an IDE like eclipse to easily understand the programs. Step 2: Add GSON dependency in pom.xml In the next step, we need to add the following GSON dependency in the pom.xml file to use the GSON library. After adding the dependency, the pom.xml file look like the following: Step 3: Create POJO to convert into JSON In the next step, we need to create a POJO, which we want to convert into a JSON object. In our example, we create a class MobilePhone with getter and setters for brand, name, ram, and rom. MobilePhone.java Step 4: Create a Java class to convert the Java object into JSON We need to create a new Java class that uses GSON to convert the MobilePhone object to JSON. In this class, we will create the getMobile() method. Inside the method, we set the values to the MobilePhone class's properties and return the MobilePhone object. In the main method, we will create an object of the MobilePhone class and store the geMobile() method's returned value. After that, we will use the Gson().toJson() method to convert that object into a JSON object. ConvertJSON.java When we run the ConvertJSON.java file, we will see the JSON object of the MobilePhone class object. Using Jackson libraryJackson library is one of the simplest Java-based libraries. The Jackson library is used to serialize the Java object to JSON and vice versa. The ObjectMapper class of the Jackson API provides methods to convert the Java object to JSON format or object. The ObjectMapper class writeValueAsString() method takes the JSON object as a parameter and returns its respective JSON string. These are the following steps to convert the Java object into a JSON object using Jackson API:
Step 1: Create a Maven project In the first step, we need to create a maven project using eclipse IDE. Just like GSON, Jackson API is also supported by the Eclipse. Step 2: Add Jackson dependency in pom.xml In the next step, we need to add the following Jackson dependency in our pom.xml file to use the Jackson library. Step 3: Create POJO to convert into JSON In the next step, we need to create a POJO, which we want to convert into a JSON object. In this example, we create a class Product having getter and setters for the properties like id, name, and price. Product.java Step 4: Create a Java class to convert the Java object into JSON Just like our previous example of GSON, we will also create a new Java class ObjectToJson. Inside the method, we have created the getObjectData() method to set the values to the Product object and return that object of the Product class. In the main() method, we have created an object of the Product class and the ObjectMapper class. After that, we will use the writeValueAsString() method of the ObjectMapper class to covert the Java object into a JSON object. ObjectToJson.java When we run the ObjectToJson.java file, we will see the JSON string of the Product class object. Both methods are used in different scenarios. Both are simple, fast and easy to use and understand.
Next TopicHow to Calculate Date Difference in Java
|