tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Interviews | Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 JSON > GOOGLE GSON > List to JSON

List to JSON 

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 converting a list to JSON string.

File Name  :  
com/bethecoder/tutorials/google_gson/tests/CollectionTest.java 
Author  :  Sudhakar KV
Email  :  [email protected]
   
package com.bethecoder.tutorials.google_gson.tests;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.bethecoder.tutorials.google_gson.common.Student;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

public class CollectionTest {

  /**
   @param args
   */
  public static void main(String[] args) {
    Gson gson = new GsonBuilder().setPrettyPrinting().create();

    List<Object> list = new ArrayList<Object>();
    list.add("ONE");
    list.add(new Integer(2));
    list.add(Boolean.TRUE);
    list.add(new Double(4.26));
    list.add(new Student("Sriram""Kasireddi"2"Singing"new Date(11046)));
    
    String jsonStr = gson.toJson(list);
    System.out.println(jsonStr);
  }

}
   

It gives the following output,
[
  "ONE",
  2,
  true,
  4.26,
  {
    "firstName": "Sriram",
    "lastName": "Kasireddi",
    "age": 2,
    "hobby": "Singing",
    "dob": "May 6, 2010 12:00:00 AM"
  }
]



 
  


  
bl  br