Importance of the accumulate() method of JSONObject in Java?

JavaJSONObject Oriented ProgrammingProgramming

A JSONObject is an unordered collection of a name and value pairs. A few important methods of JSONArray are accumulate(), put(), opt(), append(), write() and etc. The accumulate() method accumulates the values under a key and this method similar to the put() method except if there is an existing object stored under a key then a JSONArray can be stored under a key to hold all of the accumulated values. If there is an existing JSONArray then a new value can be added.

Syntax

public JSONObject accumulate(java.lang.String key, java.lang.Object value) throws JSONException

Example

import org.json.*;
public class JSONAccumulateMethodTest {
   public static void main(String[] args) throws JSONException {
      JSONObject jsonObj = new JSONObject();
      jsonObj.accumulate("Technology", "Java");
      jsonObj.accumulate("Technology", "Python");
      jsonObj.accumulate("Technology", "Spark");
      jsonObj.accumulate("Technology", "Selenium");
      jsonObj.accumulate("Technology", ".Net");
      System.out.println(jsonObj.toString(3));
   }
}

Output

{"Technology": [
   "Java",
   "Python",
   "Spark",
   "Selenium",
   ".Net"
]}
raja
Published on 21-Oct-2019 11:12:32
Advertisements