JSON Array Writer
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 to a file.
package com.bethecoder.tutorials.json_simple.tests;
import java.io.FileWriter;
import java.io.IOException;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
public class JsonArrayWriter {
/**
* @param args
* @throws IOException
*/
public static void main ( String [] args ) throws IOException {
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 ) ;
FileWriter jsonWriter = new FileWriter ( "json_array.txt" ) ;
jsonArray.writeJSONString ( jsonWriter ) ;
jsonWriter.close () ;
}
}
It gives the following output,
json_array.txt
["one","two","three",{"age":2,"name":"Sriram","hobby":"painting"}]