#JavaInspires
Hi Guys,
Welcome to Java Inspires;
In this post, we will see example to convert Java object to JSON string and JSON String to Java object.
Here, we are using Jackson databind api, and provided classes ObjectMapper to read and write json from or to java pojo.
Jackson Databind dependency in pom.xml.
<dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.12.2</version> </dependency>
MainApp.java
package com.javainspires; import java.io.IOException; import java.util.Date; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.SerializationFeature; public class MainApp { public static void main(String[] args) throws IOException { // Java Object to JSON // create java object Employee employee = new Employee(); employee.setId(110011L); employee.setName("Musk Elon"); employee.setAge(45); employee.setDoj(new Date()); Location location = new Location(2020L, "New York"); employee.setLocation(location); // create object mapper ObjectMapper objectMapper = new ObjectMapper(); objectMapper.enable(SerializationFeature.INDENT_OUTPUT); String jsonString = objectMapper.writeValueAsString(employee); System.out.println("Java Object To JSON"); System.out.println(jsonString); // now json string to java object Employee emp = objectMapper.readValue(jsonString.getBytes(), Employee.class); System.out.println(); System.out.println("JSON to Java Object"); System.out.println(emp.getId()); System.out.println(emp.getName()); System.out.println(emp.getAge()); System.out.println(emp.getDoj()); System.out.println(emp.getLocation()); } } class Employee { Long id; String name; int age; Location location; Date doj; public Employee() { super(); // TODO Auto-generated constructor stub } public Employee(Long id, String name, int age, Location location, Date doj) { super(); this.id = id; this.name = name; this.age = age; this.location = location; this.doj = doj; } public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public Location getLocation() { return location; } public void setLocation(Location location) { this.location = location; } public Date getDoj() { return doj; } public void setDoj(Date doj) { this.doj = doj; } @Override public String toString() { return "Employee [id=" + id + ", name=" + name + ", age=" + age + ", location=" + location + ", doj=" + doj + "]"; } } class Location { Long id; String name; public Location() { super(); // TODO Auto-generated constructor stub } public Location(Long id, String name) { super(); this.id = id; this.name = name; } public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "Location [id=" + id + ", name=" + name + "]"; } }
Java Object To JSON >>> { "id" : 110011, "name" : "Musk Elon", "age" : 45, "location" : { "id" : 2020, "name" : "New York" }, "doj" : 1622776845323 } JSON to Java Object >>> 110011 Musk Elon 45 Thu Jun 03 23:20:45 EDT 2021 Location [id=2020, name=New York]
Jackson Databind Reference: