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 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  :  
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("age").value("2 years")    //Add key-value pairs  
              .endObject()    //End JSON Object
            .endArray()    //End JSON Array



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


bl  br