
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
How to convert Java object to JSON using GSON 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. Google Gson is a simple Java-based library to serialize Java objects to JSON and vice versa. It is an open-source library developed by Google.
Converting Java object to JSON
The Google's Gson library provides a class with the same name (Gson) which is the main class of the library.
This class provides a method named toJson() there are several variants of this method where one among them accepts a Java object and converts it into a JSON object and returns it.
Therefore, to convert a Java object to a JSON String using GSON library −
Add the following maven dependency to your pom.xml
<dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.8.5</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 Gson class.
Invoke the toJson() 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 ObjectTOString { public static void main(String args[]) { Student std = new Student(); std.setId(001); std.setName("Krishna"); std.setAge(30); std.setPhone(9848022338L); //Creating the Gson object Gson gSon = new Gson(); String jsonString = gSon.toJson(std); System.out.println(jsonString); } }
Output
{"id":1,"name":"Krishna","age":30,"phone":9848022338}
- Related Articles
- Convert JSON object to Java object using Gson library in Java?\n
- Convert Java object to JSON using the Gson library in Java?\n
- Convert a Map to JSON using the Gson library in Java?
- How to convert Java object to JSON using Jackson library?
- Convert a list of objects to JSON using the Gson library in Java?
- How to pretty print JSON using the Gson library in Java?
- How to convert a JSON to Java Object using the Jackson library in Java?\n
- Convert a JSON String to Java Object using the json-simple library in Java?\n
- How to write a JSON string to file using the Gson library in Java?
- How to convert HASHMAP to JSON using GSON in Android?
- How to get all the keys of a JSON object using GSON in Java?
- Convert JSON to/from Map using Jackson library in Java?
- How to convert a Map to JSON object using JSON-lib API in Java?
- How to convert a List to JSON array using the Jackson library in Java?
- Convert CSV to JSON using the Jackson library in Java?\n
