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

JSON Accumulate 

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 using accumulate method of JSONObject. It accumulates the values under a key. If we put multiple values for the same key, it replaces the value with a JSONArray containing all values.

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

import org.json.JSONException;
import org.json.JSONObject;

public class JsonAccumulate {

  /**
   @param args
   @throws JSONException 
   */
  public static void main(String[] argsthrows JSONException {

    JSONObject jsonObj = new JSONObject();
    jsonObj.accumulate("certificates""SCJA");
    System.out.println(jsonObj.toString(2));
    
    jsonObj.accumulate("certificates""SCJP");
    jsonObj.accumulate("certificates""SCWCD");
    System.out.println(jsonObj.toString(2));
  }

}
   

It gives the following output,
{"certificates": "SCJA"}

{"certificates": [
  "SCJA",
  "SCJP",
  "SCWCD"
]}



 
  


  
bl  br