
- 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 pretty print JSON using the Gson library in Java?
A Gson is a JSON library for java, which is created by Google. By using Gson, we can generate JSON and convert JSON to java objects. By default, Gson can print the JSON in compact format. To enable Gson pretty print, we must configure the Gson instance using the setPrettyPrinting() method of GsonBuilder class and this method configures Gson to output JSON that fits in a page for pretty printing.
Syntax
public GsonBuilder setPrettyPrinting()
Example
import java.util.*; import com.google.gson.*; public class PrettyJSONTest { public static void main( String[] args ) { Employee emp = new Employee("Raja", "115", "Content Engineer", "Java", "Hyderabad"); Gson gson = new GsonBuilder().setPrettyPrinting().create(); // pretty print String prettyJson = gson.toJson(emp); System.out.println(prettyJson); } } // Employee class class Employee { private String name, id, designation, technology, location; public Employee(String name, String id, String designation, String technology, String location) { super(); this.name = name; this.id = id; this.designation = designation; this.technology = technology; this.location = location; } public String getName() { return name; } public String getId() { return id; } public String getDesignation() { return designation; } public String getTechnology() { return technology; } public String getLocation() { return location; } }
Output
{ "name": "Raja", "id": "115", "designation": "Content Engineer", "technology": "Java", "location": "Hyderabad" }
- Related Articles
- Pretty print JSON using Jackson library in Java?
- Pretty print JSON using org.json library in Java?\n
- Pretty print JSON using the flexjson library in Java?\n
- How to convert Java object to JSON using GSON library?
- How to pretty print json using javascript?
- Convert Java object to JSON using the Gson library in Java?\n
- Convert a Map to JSON using the Gson library in Java?
- Pretty print JSON using javax.json API in Java?\n
- How to write a JSON string to file using the Gson library in Java?
- Convert JSON object to Java object using Gson library in Java?\n
- Convert a list of objects to JSON using the Gson library in Java?
- How to format a date using the Gson library in Java?
- How to use @Until annotation using the Gson library in Java?
- How to rename the properties of JSON using Gson in Java?
- How to serialize a null field using Gson library in Java?

Advertisements