JSON Array
JSON Simple is a java library for encoding and decoding JSON text.
Get the latest binaries from
http://code.google.com/p/json-simple/ .
The following example shows serializing a simple JSON Array.
package com.bethecoder.tutorials.json_simple.tests;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
public class JsonArrayTest {
/**
* @param args
*/
public static void main ( String [] args ) {
JSONArray jsonArray = new JSONArray () ;
jsonArray.add ( "one" ) ;
jsonArray.add ( "two" ) ;
jsonArray.add ( "three" ) ;
System.out.println ( jsonArray ) ;
JSONObject jsonObj = new JSONObject () ;
jsonObj.put ( "name" , "Sriram" ) ;
jsonObj.put ( "age" , 2 ) ;
jsonObj.put ( "hobby" , "painting" ) ;
jsonArray.add ( jsonObj ) ;
System.out.println ( jsonArray ) ;
}
}
It gives the following output,
["one","two","three"]
["one","two","three",{"age":2,"name":"Sriram","hobby":"painting"}]