Last active
August 4, 2016 09:45
-
-
Save rasheedamir/3ee43e81661415b887d655d23c6e8d45 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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