JSONObjectis an unordered collection ofnamepairs andvaluepairs. Some important methods ofJSONArrayareaccumulate(), put(), opt(), append(), write(), etc.accumulate()The method accumulates the values under the key. This method is similar to theput()method, except that there are existing objects stored under the key. Then the JSONArray can be stored under the key to save all Cumulative value. If an existing JSONArray exists, new values can be added.
public JSONObject accumulate(java.lang.String key, java.lang.Object value) throws JSONException
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)); } }
{"Technology": [ "Java", "Python", "Spark", "Selenium", ".Net" ]}
The above is the detailed content of What is the importance of accumulate() method in Java?. For more information, please follow other related articles on the PHP Chinese website!