- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to convert Java object to JSON using Jackson library?
JSON or JavaScript Object Notation is a lightweight text-based open standard designed for human-readable data interchange. Conventions used by JSON are known to programmers, which include C, C++, Java, Python, Perl, etc.
There are several Java libraries available to handle JSON objects. Jackson is a simple java based library to serialize java objects to JSON and vice versa.
Converting Java object to JSON
The ObjectMapper class of the Jackson API in Java provides methods to convert a Java object to JSON object and vice versa.
The writeValueAsString() method of this class accepts a JSON object as a parameter and returns its respective JSON String
Therefore, to convert a Java object to a JSON String using Jackson library −
Add the following maven dependency to your pom.xml
<dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.10.0.pr2</version> </dependency>
Create a javabean/POJO object with private variables and setter/getter methods.
Create another class (make sure that the POJO class is available to this).
In it, create an object of the POJO class, set required values to it using the setter methods.
Instantiate the ObjectMapper class.
Invoke the writeValueAsString() method by passing the above created POJO object.
Retrieve and print the obtained JSON.
Example
import com.google.gson.Gson; class Student { private int id; private String name; private int age; private long phone; public int getId() { return id; } public void setId(int 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 long getPhone() { return phone; } public void setPhone(long phone) { this.phone = phone; } } public class JacksionExample { public static void main(String args[]) throws Exception { Student std = new Student(); std.setId(001); std.setName("Krishna"); std.setAge(30); std.setPhone(9848022338L); //Creating the ObjectMapper object ObjectMapper mapper = new ObjectMapper(); //Converting the Object to JSONString String jsonString = mapper.writeValueAsString(std); System.out.println(jsonString); } }
Output
{"id":1,"name":"Krishna","age":30,"phone":9848022338}
- Related Articles
- How to convert a JSON to Java Object using the Jackson library in Java?\n
- Convert JSON to/from Map using Jackson library in Java?
- Convert CSV to JSON using the Jackson library in Java?\n
- How to convert Java object to JSON using GSON library?
- How to convert a List to JSON array using the Jackson library in Java?
- How to convert a JSON object to an enum using Jackson in Java?
- Convert JSON object to Java object using Gson library in Java?\n
- How to ignore a field of JSON object using the Jackson library in Java?\n
- Pretty print JSON using Jackson library in Java?
- Convert Java object to JSON using the Gson library in Java?\n
- Convert a JSON String to Java Object using the json-simple library in Java?\n
- Convert POJO to XML using the Jackson library in Java?
- Convert XML to POJO using the Jackson library in Java?
- How to convert a Map to JSON object using JSON-lib API in Java?
- How can we convert a JSON array to a list using Jackson in Java?\n
