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

Simple 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 Object using JSONStringer.

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

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

public class SimpleStringer {

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

    System.out.println(jsonStr);
  }

}
   

It gives the following output,
{ "name":"Sriram", "age":"2 years", "hobby":"painting" }



 
  


  
bl  br