Pretty Print
Google-gson is a java library from Google for encoding and decoding JSON text.
Get the latest binaries from
http://code.google.com/p/google-gson/ .
The following example shows enabling pretty printing.
package com.bethecoder.tutorials.google_gson.tests;
import java.util.Date;
import com.bethecoder.tutorials.google_gson.common.Student;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
public class PrettyPrint {
/**
* @param args
*/
public static void main ( String [] args ) {
Gson gson = new GsonBuilder () .setPrettyPrinting () .create () ;
Student stud = new Student ( "Sriram" , "Kasireddi" , 2 , "Singing" , new Date ( 110 , 4 , 6 )) ;
System.out.println ( gson.toJson ( stud )) ;
}
}
It gives the following output,
{
"firstName": "Sriram",
"lastName": "Kasireddi",
"age": 2,
"hobby": "Singing",
"dob": "May 6, 2010 12:00:00 AM"
}