Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Importance of the accumulate() method of JSONObject in Java?
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"
]}Advertisements