tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Interviews | Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 JSON > JSON SIMPLE > JSON Array Writer

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.

File Name  :  
com/bethecoder/tutorials/json_simple/tests/JsonArrayWriter.java 
Author  :  Sudhakar KV
Email  :  [email protected]
   
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[] argsthrows 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"}]



 
  


  
bl  br