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

JSON Accumulate 

JSON-lib is a java library for serializing and de-serializing java beans, maps, arrays and collections in JSON format. Get the latest binaries from sourceforge http://json-lib.sourceforge.net/. This requires the libraries ( json-lib-2.4-jdk15.jar, xom-1.2.7.jar, ezmorph.jar commons-lang.jar, commons-collections.jar, commons-beanutils-1.7.jar and commons-logging-1.1.1.jar) to be in classpath. 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_lib/tests/JsonAccumulate.java 
Author  :  Sudhakar KV
Email  :  kvenkatasudhakar@gmail.com
   
package com.bethecoder.tutorials.json_lib.tests;

import net.sf.json.JSONObject;

public class JsonAccumulate {

  /**
   @param args
   */
  public static void main(String[] args) {

    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))//pretty print with indent
  }

}
   

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

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


                    Print

   



Books Recommendation

 
 
bl  br