JSONArray jsonArray = new JSONArray();

for (loop) {
    JSONObject jsonObj= new JSONObject();
    jsonObj.put("srcOfPhoto", srcOfPhoto);
    jsonObj.put("username", "name"+count);
    jsonObj.put("userid", "userid"+count);

    jsonArray.put(jsonObj.valueToString());
}

JSONObject parameters = new JSONObject();

parameters.put("action", "remove");

parameters.put("datatable", jsonArray );

parameters.put(Constant.MSG_TYPE , Constant.SUCCESS);

Why were you using an Hashmap if what you wanted was to put it into a JSONObject?

EDIT: As per http://www.json.org/javadoc/org/json/JSONArray.html

EDIT2: On the JSONObject method used, I'm following the code available at: https://github.com/stleary/JSON-java/blob/master/JSONObject.java#L2327 , that method is not deprecated.

We're storing a string representation of the JSONObject, not the JSONObject itself

Answer from Gonçalo Vieira on Stack Overflow
🌐
Stack Overflow
stackoverflow.com › questions › 50039189 › how-to-add-object-to-jsonarray
java - How to add object to JSONArray - Stack Overflow
April 26, 2018 - public class Test { public static void main(String[] args) { Mypojo mypojo = new Mypojo(); Gson gson = new Gson(); JSONArray records = new JSONArray(); for (int i = 0; i < 1; i++) { if (5 > 0) { mypojo.setPoAccount("050017"); mypojo.setPoAmount("12"); JSONObject objects = new JSONObject(gson.toJson(mypojo)); records.put(objects); } mypojo.setPoAccount("050016"); mypojo.setPoAmount("800"); JSONObject objects = new JSONObject(gson.toJson(mypojo)); records.put(objects); } System.out.println(records); } }
🌐
TutorialsPoint
tutorialspoint.com › how-can-we-add-a-jsonarray-within-jsonobject-in-java
How can we add a JSONArray within JSONObject in Java?
A JSONObject can parse text from ... object. We can also add a JSONArray within JSONObject by first creating a JSONArray with few items and add these array of items to the put() method of JSONObject class. public JSONObject put(java.lang.String key, java.util.Collection<?> ...
🌐
TutorialsPoint
tutorialspoint.com › how-can-we-add-a-jsonarray-to-jsonobject-in-java
How can we add a JSONArray to JSONObject in Java?
July 4, 2020 - We need to add a few items to an ArrayList first and pass this list to the put() method of JSONArray class and finally add this array to JSONObject using the put() method. import org.json.*; import java.util.*; public class AddJSONArrayToJSONObjTest { public static void main(String args[]) ...
🌐
Tabnine
tabnine.com › home page › code › java › org.json.jsonarray
org.json.JSONArray.put java code examples | Tabnine
April 28, 2025 - SharedPreferences prefs = PreferenceManager .getDefaultSharedPreferences(this); JSONArray jsonArray = new JSONArray(); jsonArray.put(1); jsonArray.put(2); Editor editor = prefs.edit(); editor.putString("key", jsonArray.toString()); System.out.println(jsonArray.toString()); editor.commit(); ... public void setOnJSON(JSONObject json, String key, Object value) throws JSONException { JSONArray jsonArray = new JSONArray(); for (String stringValue : (String[])value) { jsonArray.put(stringValue); } json.put(key, jsonArray); } }); ... private JSONArray convertListToJsonArray(Object value) throws InvocationTargetException, IllegalAccessException { JSONArray array = new JSONArray(); List<Object> list = (List<Object>) value; for(Object obj : list) { // Send null, if this is an array of arrays we are screwed array.put(obj != null ?
Find elsewhere
🌐
TutorialsPoint
tutorialspoint.com › how-to-write-create-a-json-array-using-java
How to Write/create a JSON array using Java?
September 6, 2023 - //Creating a JSONObject object ... class. jsonObject.put("key", "value"); Create a JSON array by instantiating the JSONArray class and add, elements to the created array using the add() method of the JSONArray class....
🌐
Quora
quora.com › How-do-you-add-elements-to-a-JSON-Array
How to add elements to a JSON Array - Quora
Answer (1 of 3): Here’s how you add elements to a JSON array—let’s break it down like you’re sitting around a table with a laptop, trying to figure this out. So, first off, JSON arrays are those square-bracket lists of stuff, right? Like `["apple", "banana"]`. To add something, you ...
🌐
Oracle
docs.oracle.com › javaee › 7 › api › javax › json › JsonArray.html
JsonArray (Java(TM) EE 7 Specification APIs)
The example code below demonstrates how to create the following JSON array: [ { "type": "home", "number": "212 555-1234" }, { "type": "fax", "number": "646 555-4567" } ] JsonArray value = Json.createArrayBuilder() .add(Json.createObjectBuilder() .add("type", "home") .add("number", "212 555-1234")) .add(Json.createObjectBuilder() .add("type", "fax") .add("number", "646 555-4567")) .build(); The following example demonstrates how to write a JsonArray object as JSON data:
🌐
Tabnine
tabnine.com › home page › code › java › org.json.jsonobject
org.json.JSONObject.append java code examples | Tabnine
November 21, 2017 - JSONObject result = new JSONObject(); JSONArray array1 = new JSONArray(); for (List<Map<String, String>> tmpList : finalOutput) { JSONArray array2 = new JSONArray(); for (int i = 0; i < tmpList.size(); i++) { Map<String, String> insideMap = tmpList.get(i); JSONObject obj = new JSONObject(insideMap); array2.add(obj); } array1.add(array2); } result.append("result", array1); return result.toString(); origin: NationalSecurityAgency/lemongrenade · /** * Sets a job to STOPPED and then deletes it. Endpoint available at /rest/v2/delete/{id} * @param jobId * @return Returns a Response object with body as standard job return format containing result of delete request.
🌐
Automation Anywhere
community.automationanywhere.com › home › forums › developers forum › how can i add an element to a json array using the json object manager
How can I add an element to a JSON array using the JSON Object Manager | Community
April 4, 2022 - I dont think you can do it in the JSON Object Manager package...I believe you can only set values there, not actually adding elements...I think the easiest way to do it in Automation 360 would be to have a small JavaScript function that could do it.
🌐
IBM
ibm.com › support › pages › creating-json-string-json-object-and-json-arrays-automation-scripts
Creating a JSON String from JSON Object and JSON Arrays in Automation Scripts
# creating a JSON String with an array (directly executed via Run Automation Script button) from com.ibm.json.java import JSONObject, JSONArray from sys import * # method for creating a JSON formatted String including an array within def createJSONstring(): # defining the first child object ch1_obj = JSONObject() ch1_obj.put('CH_FIELD_1', 1) ch1_obj.put('CH_FIELD_2', 'VALUE_2') # defining the second child object ch2_obj = JSONObject() ch2_obj.put('CH_FIELD_1', 2) ch2_obj.put('CH_FIELD_2', 'VALUE_3') # adding child objects to children array ch_arr = JSONArray() ch_arr.add(ch1_obj) ch_arr.add(ch
🌐
Progress
docs.progress.com › bundle › abl-reference › page › Add-method-JsonArray.html
Add( ) method (JsonArray)
Skip to main contentSkip to search · Powered by Zoomin Software. For more details please contactZoomin
🌐
Processing
processing.org › reference › JSONArray_append_.html
JSONArray - append() / Reference / Processing.org
August 1, 2025 - Appends a new value to the JSONArray, increasing the array's length by one. New values may be of the following types: int, float, String, boolean, JSONObject,…
🌐
TutorialsPoint
tutorialspoint.com › how-to-add-elements-to-json-object-using-json-lib-api-in-java
How to add elements to JSON Object using JSON-lib API in Java?
public JSONObject element(String key, Object value) - put a key/value pair in the JSONObject · import java.util.Arrays; import net.sf.json.JSONObject; public class JsonAddElementTest { public static void main(String[] args) { JSONObject jsonObj = new JSONObject() .element("name", "Raja Ramesh") .element("age", 30) .element("address", "Hyderabad") .element("contact numbers", Arrays.asList("9959984000", "7702144400", "7013536200")); System.out.println(jsonObj.toString(3)); //pretty print JSON } }
🌐
Stack Overflow
stackoverflow.com › questions › 34854560 › java-jsonobject-adding-a-new-element-in-arrays-jsonarray
JAVA JSONObject adding a new element in Arrays (JSONArray) - Stack Overflow
Bring the best of human thought and AI automation together at your work. Explore Stack Internal ... I need add an element into JSONArray, but the method “append” creates multiples arrays instead add one element.