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

Array Stringer 

JSON (JavaScript Object Notation) is a lightweight text-based open standard designed for human-readable data interchange. Douglas Crockford has provided a reference implementation of JSON in Java at http://json.org/ useful for JSON serialization and deserialization. Click here to download the compiled library. The following example shows serializing a JSON Array using JSONStringer.

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

import org.json.JSONException;
import org.json.JSONStringer;

public class ArrayStringer {

  /**
   @param args
   @throws JSONException 
   */
  public static void main(String[] argsthrows JSONException {
    JSONStringer stringer = new JSONStringer();
    
    String jsonStr = stringer
        .array()    //Start JSON Array
              .object()      //Start JSON Object
                .key("name").value("Sriram")
                .key("age").value("2 years")    //Add key-value pairs  
                .key("hobby").value("painting")
              .endObject()    //End JSON Object
              
              .object()
                .key("foo").value("bar")
                .key("hello").value("world")
              .endObject()
            .endArray()    //End JSON Array
            .toString();

    System.out.println(jsonStr);
  }

}
   

It gives the following output,
[ {"name":"Sriram","age":"2 years","hobby":"painting"}, {"foo":"bar","hello":"world"} ]



 
  


  
bl  br