Skip to content

Instantly share code, notes, and snippets.

@rasheedamir
Last active August 4, 2016 09:45
Show Gist options
  • Save rasheedamir/3ee43e81661415b887d655d23c6e8d45 to your computer and use it in GitHub Desktop.
Save rasheedamir/3ee43e81661415b887d655d23c6e8d45 to your computer and use it in GitHub Desktop.
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
public class PrettyPrintJson
{
public static void main(String[] args)
{
Person person = new Person();
person.firstName = "John";
person.lastName = "Stark";
person.age = 32;
person.nationality = "British";
Gson gson = new Gson();
String json = gson.toJson(person);
System.out.println(json);
//// OUTPUT
/* {"firstName":"John","lastName":"Stark","age":32,"nationality":"British"} */
Gson prettyGson = new GsonBuilder().setPrettyPrinting().create();
String prettyJson = prettyGson.toJson(person);
System.out.println(prettyJson);
//// OUTPUT
/*
{
"firstName": "John",
"lastName": "Stark",
"age": 32,
"nationality": "British"
}
*/
}
}
class Person
{
public String firstName;
public String lastName;
public int age;
public String nationality;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment