🌐
Baeldung
baeldung.com › home › json › getting a value in jsonobject
Getting a Value in JSONObject | Baeldung
May 5, 2025 - First, when the value of a key is of JSONObject or JSONArray type, we need to propagate the recursive search down in that value. Second, when the key is found in the current recursive call, we need to add its mapped value to the returned result, regardless of whether the value is of a primitive type or not. ... public List<String> getValuesInObject(JSONObject jsonObject, String key) { List<String> accumulatedValues = new ArrayList<>(); for (String currentKey : jsonObject.keySet()) { Object value = jsonObject.get(currentKey); if (currentKey.equals(key)) { accumulatedValues.add(value.toString())
🌐
Stleary
stleary.github.io › JSON-java › org › json › JSONObject.html
JSONObject
Produce a JSONArray containing the values of the members of this JSONObject. ... names - A JSONArray containing a list of key strings.
🌐
Stack Overflow
stackoverflow.com › questions › 42835038 › sending-list-of-values-in-jsonobject
java - sending list of values in JSONObject - Stack Overflow
Then at the receiving end just write something to loop through and get all values Example: public class sample { public static void main(String[] args) { List<String> sList = new ArrayList<String>(); JSONObject inputJsonObj = new JSONObject(); try { inputJsonObj.put("ipaddress","10.254.27.12"); inputJsonObj.put("ipaddress1", "10.253.140.116"); // Add one to the name Client client = Client.create(); WebResource webResource = client .resource("http://10.85.249.29:8080/checkRest/CheckxARC/getVersion"); ClientResponse response = webResource.type("application/json") .post(ClientResponse.class, inputJsonObj.toString()); String output = response.getEntity(String.class); System.out.println(" op--->"+output); } catch (Exception e) { e.printStackTrace(); } } Get all IP addresses: Collection values = jObject.values(); values.forEach( System.out::print ); Edited errors in code.
🌐
Medium
medium.com › @papapapaya11 › exploring-various-ways-to-handle-jsonobject-in-java-6edef1f0561c
Exploring Various Ways to Handle JSONObject in Java | by Little Miss Brave | Medium
December 2, 2024 - List<JSONObject> jsonList = new ArrayList<>(); jsonList.add(new JSONObject().put("id", 1).put("value", "A")); jsonList.add(new JSONObject().put("id", 2).put("value", "B")); for (Object obj : jsonList) { System.out.println("ID: " + obj.getInt("id")); ...
Top answer
1 of 5
20

Call getJSONObject() instead of getString(). That will give you a handle on the JSON object in the array and then you can get the property off of the object from there.

For example, to get the property "value" from a List<SomeClass> where SomeClass has a String getValue() and setValue(String value):

JSONObject obj = new JSONObject();
List<SomeClass> sList = new ArrayList<SomeClass>();

SomeClass obj1 = new SomeClass();
obj1.setValue("val1");
sList.add(obj1);

SomeClass obj2 = new SomeClass();
obj2.setValue("val2");
sList.add(obj2);

obj.put("list", sList);

JSONArray jArray = obj.getJSONArray("list");
for(int ii=0; ii < jArray.length(); ii++)
  System.out.println(jArray.getJSONObject(ii).getString("value"));
2 of 5
3

Let us assume that the class is Data with two objects name and dob which are both strings.

Initially, check if the list is empty. Then, add the objects from the list to a JSONArray

JSONArray allDataArray = new JSONArray();
List<Data> sList = new ArrayList<String>();

    //if List not empty
    if (!(sList.size() ==0)) {

        //Loop index size()
        for(int index = 0; index < sList.size(); index++) {
            JSONObject eachData = new JSONObject();
            try {
                eachData.put("name", sList.get(index).getName());
                eachData.put("dob", sList.get(index).getDob());
            } catch (JSONException e) {
                e.printStackTrace();
            }
            allDataArray.put(eachData);
        }
    } else {
        //Do something when sList is empty
    }
    

Finally, add the JSONArray to a JSONObject.

JSONObject root = new JSONObject();
    try {
        root.put("data", allDataArray);
    } catch (JSONException e) {
        e.printStackTrace();
    }

You can further get this data as a String too.

String jsonString = root.toString();
🌐
How to do in Java
howtodoinjava.com › home › convert json array to list: gson, jackson and org.json
Convert JSON Array to List: Gson, Jackson and Org.json
September 25, 2023 - JSONObject represents a person object in the JSON. List<Person> readPersonListFromJsonArray(String json) { JSONArray jsonArray = new JSONArray(json); List<Person> personList = new ArrayList<>(); for (int i = 0; i < jsonArray.length(); i++) { JSONObject jsonPerson = jsonArray.getJSONObject(i); long id = jsonPerson.getLong("id"); String name = jsonPerson.getString("name"); int age = jsonPerson.getInt("age"); Person person = new Person(id, name, age); personList.add(person); } return personList; }
🌐
Processing
processing.org › reference › jsonobject
JSONObject / Reference / Processing.org
JSONObject json; void setup() { json = new JSONObject(); json.setInt("id", 0); json.setString("species", "Panthera leo"); json.setString("name", "Lion"); saveJSONObject(json, "data/new.json"); } // Sketch saves the following to a file called "new.json": // { // "id": 0, // "species": "Panthera leo", // "name": "Lion" // } getString()Gets the String value associated with the specified key
Find elsewhere
🌐
GitHub
github.com › stleary › JSON-java › issues › 491
Casting list of JSONObject to JSONObject throws ClassCastException · Issue #491 · stleary/JSON-java
October 22, 2019 - @Test public void shouldCastArrayOfJSONObjectsToList() { String a = "{\"a\": 1}"; String b = "{\"b\":2}"; String jsonString = "{\"list\":[" + a + "," + b + "]}"; JSONObject jsonObject = new JSONObject(jsonString); JSONArray jsonArray = jsonObject.getJSONArray("list"); List<JSONObject> jsonObjects = jsonArray.toList().stream().map(JSONObject.class::cast).collect(Collectors.toList()); assertEquals(jsonObjects.size(), 2); }
Author   ggavriilidis
🌐
Oracle
docs.oracle.com › javaee › 7 › api › javax › json › JsonObject.html
JsonObject (Java(TM) EE 7 Specification APIs)
JsonWriter writer = ... JsonObject obj = ...; writer.writeObject(obj); JsonObject values can be JsonObject, JsonArray, JsonString, JsonNumber, JsonValue.TRUE, JsonValue.FALSE, JsonValue.NULL.
🌐
Android Developers
developer.android.com › api reference › jsonobject
JSONObject | API reference | Android Developers
Skip to main content · English · Deutsch · Español – América Latina · Français · Indonesia · Polski · Português – Brasil · Tiếng Việt · 中文 – 简体
🌐
Kotlin Discussions
discuss.kotlinlang.org › support
How to get listitems in JSONObject - Support - Kotlin Discussions
May 18, 2022 - Hello guys, my JSON is supposed to look like this. I am using import org.json.JSONObject {"zip":123, "people":[{"firstname":"Thomas", "lastname":"Tatum"}, {"firstname":"Drew", "lastname":"Uncle"}]} I have a MutableList, in the List are Person (it’s a data class with firstname and lastname).
🌐
Baeldung
baeldung.com › home › json › get a value by key in a jsonarray
Get a Value by Key in a JSONArray | Baeldung
January 8, 2024 - So, let’s find all values for a given key: public List<String> getValuesForGivenKey(String jsonArrayStr, String key) { JSONArray jsonArray = new JSONArray(jsonArrayStr); return IntStream.range(0, jsonArray.length()) .mapToObj(index -> ((JSONObject)jsonArray.get(index)).optString(key)) .collect(Collectors.toList()); }
🌐
Mkyong
mkyong.com › home › java › how to parse json array with jackson
How to parse JSON Array with Jackson - Mkyong.com
April 23, 2024 - convert JSON array to Array objects Person[] person1 = mapper.readValue(jsonArray, Person[].class); for (Person p : person1) { System.out.println(p); } // 2. convert JSON array to List List<Person> person2 = mapper.readValue(jsonArray, new TypeReference<>() { }); person2.forEach(System.out::println); } }
🌐
Baeldung
baeldung.com › home › json › jackson › convert json array to java list
Convert JSON Array to Java List | Baeldung
August 13, 2025 - Gson is a widely-used JSON library for serializing and deserializing Java objects to and from JSON. It offers a simple method to change a JSON array into a List object.
🌐
Stleary
stleary.github.io › JSON-java › org › json › JSONArray.html
JSONArray
Its external text form is a string ... for accessing the values by index, and put methods for adding or replacing values. The values can be any of these types: Boolean, JSONArray, JSONObject, Number, String, or the JSONObject.NULL object....
🌐
PyPI
pypi.org › project › jsonobject
jsonobject · PyPI
Notice how an underscore is present in the python property name ('from_'), but absent in the JSON property name ('from'). \*If you're wondering how `StringProperty`'s `name` parameter could possibly default to `to` in the example above, when it doesn't have access to the `Route` class's properties at init time, you're completely right. The behavior described is implemented in `JsonObject`'s `__metaclass__`, which *does* have access to the `Route` class's properties. ... A list of allowed values for the property.
      » pip install jsonobject
    
Published   Feb 27, 2025
Version   2.3.1
🌐
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 - import org.json.*; import java.util.*; public class AddJSONArrayToJSONObjTest { public static void main(String args[]) { List<String> list = new ArrayList<String>(); list.add("Raja"); list.add("Jai"); list.add("Adithya"); JSONArray array = new JSONArray(); for(int i = 0; i < list.size(); i++) { array.put(list.get(i)); } JSONObject obj = new JSONObject(); try { obj.put("Employee Names:", array); } catch(JSONException e) { e.printStackTrace(); } System.out.println(obj.toString()); } }
🌐
Atlassian Community
community.atlassian.com › q&a › atlassian automation › questions › converting list to json object
Solved: Converting list to JSON object
December 5, 2023 - I have a variable (mylist) with list such as 50810, 50811 which is populated from a web request. I was able to convert it into an array like ["50810", "50811"] using .asJsonStringArray Ultimately, I want to convert it into JSON object like [{ "mykey" : "50810"}, {"mykey" : "50811"}] so I can updat...