JSON stands for JavaScript Object Notation. It’s a standard text-based format that shows structured data based on JavaScript object syntax. It is commonly used for transmitting data in web applications. JSON is highly recommended to transmit data between a server and web application. In order to convert a Java object into JSON, the following two methods can be used that are as listed below as follows:
- GSON
- JACKSON API
Java object is converted into JSON using Jackson API.
Steps to Convert Java Object to JSON String
Step 1: Add jar files of Jackson (in the case of the Maven project add Jackson dependencies in the pom.xml file)
< dependency >
< groupId >com.fasterxml.jackson.core</ groupId >
< artifactId >jackson-databind</ artifactId >
< version >2.5.3</ version >
</ dependency >
|
Now pom.xml file is depicted below as follows:
File: Geeks/pom.xml
Step 2: Create a POJO (Plain Old Java Object) to be converted into JSON
// Java Program to Illustrate Organisation Class package com.Geeks;
// Importing required classes public class Organisation {
// Class data members
private String organisation_name;
private String description;
private int Employees;
// Calling getters and setters
// Getter
public String getOrganisation_name()
{
return organisation_name;
}
// Setter
public void
setOrganisation_name(String organisation_name)
{
this .organisation_name = organisation_name;
}
// Getter
public String getDescription() { return description; }
// Setter
public void setDescription(String description)
{
this .description = description;
}
// Getter
public int getEmployees() { return Employees; }
// Setter
public void setEmployees( int employees)
{
Employees = employees;
}
// Method
// Creating toString
@Override public String toString()
{
// Returning attributes of organisation
return "Organisation [organisation_name="
+ organisation_name
+ ", description=" + description
+ ", Employees=" + Employees + "]" ;
}
} |
Step 3: Create a Java class for converting the Organisation object into JSON.
Convert the object into JSON using ObjectMapper class of Jackson API.
// Java Program to Illustrate Object to JSON Conversion package com.Geeks;
// Importing required classes import com.Geeks.Organisation;
import java.io.IOException;
import org.codehaus.jackson.map.ObjectMapper;
// Class public class ObjectToJson {
// Main driver method
public static void main(String[] a)
{
// Creating object of Organisation
Organisation org = new Organisation();
// Insert the data into the object
org = getObjectData(org);
// Creating Object of ObjectMapper define in Jackson
// Api
ObjectMapper Obj = new ObjectMapper();
// Try block to check for exceptions
try {
// Getting organisation object as a json string
String jsonStr = Obj.writeValueAsString(org);
// Displaying JSON String on console
System.out.println(jsonStr);
}
// Catch block to handle exceptions
catch (IOException e) {
// Display exception along with line number
// using printStackTrace() method
e.printStackTrace();
}
}
// Method
// Getting the data to be inserted
// into the object
public static Organisation
getObjectData(Organisation org)
{
// Insert the custom data
org.setName( "GeeksforGeeks" );
org.setDescription(
"A computer Science portal for Geeks" );
org.setEmployees( 2000 );
// Returning the object
return org;
}
|
Step 3: Execute the process.
The output in the JSON will be as below:
Output: