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 Object to a file.
package com.bethecoder.tutorials.json_simple.tests; import java.io.FileWriter; import java.io.IOException; import org.json.simple.JSONObject; public class JsonWriter { /** * @param args * @throws IOException */ public static void main(String[] args) throws IOException { JSONObject obj = new JSONObject(); obj.put("name", "Sriram"); obj.put("age", 2); obj.put("hobby", "painting"); FileWriter jsonWriter = new FileWriter("json.txt"); obj.writeJSONString(jsonWriter); jsonWriter.close(); } }
json.txt {"age":2,"name":"Sriram","hobby":"painting"}