
The ObjectMapper class from the com.fasterxml.jackson.databind is the class that can be used to convert Java Objects (mostly POJOs) to JSON and vice-versa.
To make use of the ObjectMapper, you will need to add the jackson-databind dependency in your Gradle/Maven Project,
How to add Jackson Dependecy in Gralde Project
How to add Jackson Dependecy in Maven Project
Example: ObjectMapper to convert JSON String to Java Object
Employee.javapublic class Employee {
String empName;
int empAge;
String empDept;
public Employee() {}
public Employee(String empName, int empAge, String empDept) {
this.empName = empName;
this.empAge = empAge;
this.empDept = empDept;
}
public String getEmpName() {
return empName;
}
public void setEmpName(String empName) {
this.empName = empName;
}
public int getEmpAge() {
return empAge;
}
public void setEmpAge(int empAge) {
this.empAge = empAge;
}
public String getEmpDept() {
return empDept;
}
public void setEmpDept(String empDept) {
this.empDept = empDept;
}
}
import com.fasterxml.jackson.databind.ObjectMapper;
public class ObjectMapperExample {
public static void main(String[] args) {
try {
ObjectMapper objectMapper = new ObjectMapper();
String employeeJson = "{ \"empName\" : \"Jake\", \"empAge\" : 35, \"empDept\" : \"Insurance\"}";
Employee employee = objectMapper.readValue(employeeJson, Employee.class);
System.out.println("Employee Name:" + employee.getEmpName());
System.out.println("Employee Department:" + employee.getEmpDept());
} catch (Exception e) {
e.printStackTrace();
}
}
}
Example: ObjectMapper to convert Java Object to Json String
public static void main(String[] args) {
try {
Employee employee = new Employee("Jake",45,"Finance");
ObjectMapper objectMapper = new ObjectMapper();
String employeeJsonString = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(employee);
System.out.println("Employee Json :" + employeeJsonString);
} catch (Exception e) {
e.printStackTrace();
}
}
Output:
Employee Json :{
"empName" : "Jake",
"empAge" : 45,
"empDept" : "Finance"
}
-
Facing issues? Have Questions? Post them here! I am happy to answer!
More Posts related to Java,
- Java equals method - Tutorial
- Unbound classpath container: JRE System Library [JavaSE-1.7]
- Spring Boot: @RequestBody not applicable to method
- Java 8: Steam map with Code Examples
- Java Program: Random Number Generator
- Java java.time.Clock class code examples [Java Date Time API]
- Fix: type argument is not within bounds of type-variable T
- [Fix] java.net.MalformedURLException: unknown protocol
- Java 7 addSuppression() and getSuppression() Exception Handling
- Convert Java Array to ArrayList Code Example
- How to Word-Warp Console logs in IntelliJ
- Ways Compare Dates in Java Programming with Examples
- Remove Trailing zeros BigDecimal Java
- CRUD operations in Spring Boot + JDBC
- [Java Threads] Should we extend Thread Class or implement Runnable interface
- Json Serialization and Deserialization using Java Jackson
- Create simple struts2 project using maven commands
- How to install Java OpenJDK 11 on Alpine Linux
- Unsupported major.minor version 52.0 in java
- Error: Can not find the tag library descriptor for
- Java: Convert String to Binary
- How to run Java Unit Test cases with Apache Maven?
- Java: Testing Private Methods in JUnit using reflection API Example
- Java JDBC Connection with MySQL Driver in VS Code + Troubleshooting
- Java Join Strings with Comma Separator
More Posts:
- Trace using cURL Command Example - cURL
- How to create MD5 digest in Notepad++ - NotepadPlusPlus
- Enable Native Dark Mode in Notepad++ - NotepadPlusPlus
- How to add days to a date in Python - Python
- Java Stream flatmap() Examples - Java
- See actual SharePoint error exception modify web.config - SharePoint
- Sublime Text 3 spell check shortcut - Sublime
- 18: Get Sub List By Slicing a Python List - 1000+ Python Programs - Python-Programs