Assuming that you are using org.json, I would iterate over the array as follows:

public static void main(String[] args) {
    String json = "{ \"data\": { \"current_condition\": [ {\"cloudcover\": \"75\", \"humidity\": \"71\", \"observation_time\": \"06:55 AM\", \"precipMM\": \"0.6\", \"pressure\": \"1009\", \"temp_C\": \"32\", \"temp_F\": \"90\", \"visibility\": \"10\", \"weatherCode\": \"116\", \"weatherDesc\": [ {\"value\": \"Partly Cloudy\" } ], \"weatherIconUrl\": [ {\"value\": \"http:\\/\\/cdn.worldweatheronline.net\\/images\\/wsymbols01_png_64\\/wsymbol_0002_su‌​nny_intervals.png\" } ], \"winddir16Point\": \"S\", \"winddirDegree\": \"170\", \"windspeedKmph\": \"9\", \"windspeedMiles\": \"6\" } ]}}";
    try {
        JSONObject jObj = new JSONObject(json);
        JSONObject dataResult = jObj.getJSONObject("data");
        JSONArray jArr = (JSONArray) dataResult.getJSONArray("current_condition");
        for(int i = 0; i < jArr.length();i++) {
            JSONObject innerObj = jArr.getJSONObject(i);
            for(Iterator it = innerObj.keys(); it.hasNext(); ) {
                String key = (String)it.next();
                System.out.println(key + ":" + innerObj.get(key));
            }
        }
    }
    catch (JSONException e) {
        e.printStackTrace();
    }

}
Answer from Prior99 on Stack Overflow
🌐
Kodejava
kodejava.org › how-do-i-pretty-print-json-string-in-json-java
How do I pretty print JSON string in JSON-Java? - Learn Java by Examples
Let’s create a pretty-printed JSONObject text using the code below. package org.kodejava.json; import org.json.JSONArray; import org.json.JSONObject; public class PrettyPrintJSON { public static void main(String[] args) { JSONObject jsonObject = new JSONObject(); jsonObject.put("id", 1L); jsonObject.put("name", "Alice"); jsonObject.put("age", 20); JSONArray courses = new JSONArray( new String[]{"Engineering", "Finance"}); jsonObject.put("courses", courses); // Default print without indent factor System.out.println(jsonObject); // Pretty print with 2 indent factor System.out.println(jsonObject.toString(2)); } }
🌐
Tabnine
tabnine.com › home page › code › java › org.json.jsonarray
org.json.JSONArray.toString java code examples | Tabnine
@Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { Query post = RemoteAccess.evaluate(request); // manage DoS if (post.isDoS_blackout()) {response.sendError(503, "your request frequency is too high"); return;} // evaluate get parameters String data = post.get("data", ""); try { String json = data; JSONArray array = CDL.toJSONArray(json); PrintWriter sos = response.getWriter(); sos.print(array.toString(2)); sos.println(); } catch (IOException e) { DAO.severe(e); JSONObject json = new JSONObject(true); json.put("error", "Malformed CSV.
🌐
Lenar
lenar.io › print-object-content-java-json
How to print object as JSON in Java | Lenar.io
Now we’re going to print our Java object as JSON · List<String> chapters = Arrays.asList(new String[]{"The way Java works", "Code structure in Java", "Making your first Java objects", "Java keywords"}); Book book = new Book("Head First Java", "Kathy Sierra, Bert Bates", 688, chapters); System.out.println(book); Readable console output in JSON format ·
🌐
TutorialsPoint
tutorialspoint.com › how-to-read-parse-json-array-using-java
How to read/parse JSON array using Java?
The iterator() method of the JSONArray class returns an Iterator object, using which you can iterate through the contents of the current array. Iterator<String> iterator = jsonArray.iterator(); while(iterator.hasNext()) { System.out.println(iterator.next()); } Below is an example of a Java program that parses the above-created sample.json file, reads its contents, and displays them.
🌐
Aviator Dao
java2novice.com › главная страница › welcome to aviator dao from the creators of the java2novice
How to read Json array data using JsonArray? - Java API ...
July 17, 2024 - Discover the evolution of our journey from Java programming tutorials to the exciting world of the Aviator Game. At Aviator DAO, we provide in-depth guides, strategies, and resources for mastering Aviator.
🌐
Processing
processing.github.io › processing-javadocs › core › processing › data › JSONArray.html
JSONArray
Make a pretty-printed JSON text of this JSONArray. Warning: This method assumes that the data structure is acyclical. ... indentFactor - The number of spaces to add to each level of indentation. Use -1 to specify no indentation and no newlines. ... a printable, displayable, transmittable representation of the object, beginning with [ (left bracket) and ending with ] (right bracket). public java.lang.String join(java.lang.String separator)
🌐
Stleary
stleary.github.io › JSON-java › org › json › JSONArray.html
JSONArray
indentFactor > 0 and the JSONArray has only one element, then the array will be output on a single line: ... Warning: This method assumes that the data structure is acyclical. ... a printable, displayable, transmittable representation of the object, beginning with [ (left bracket) and ending with ] (right bracket).
Top answer
1 of 3
58

for your example:

{'profiles': [{'name':'john', 'age': 44}, {'name':'Alex','age':11}]}

you will have to do something of this effect:

JSONObject myjson = new JSONObject(the_json);
JSONArray the_json_array = myjson.getJSONArray("profiles");

this returns the array object.

Then iterating will be as follows:

    int size = the_json_array.length();
    ArrayList<JSONObject> arrays = new ArrayList<JSONObject>();
    for (int i = 0; i < size; i++) {
        JSONObject another_json_object = the_json_array.getJSONObject(i);
            //Blah blah blah...
            arrays.add(another_json_object);
    }

//Finally
JSONObject[] jsons = new JSONObject[arrays.size()];
arrays.toArray(jsons);

//The end...

You will have to determine if the data is an array (simply checking that charAt(0) starts with [ character).

Hope this helps.

2 of 3
1

You can prefer quick-json parser to meet your requirement...

quick-json parser is very straight forward, flexible, very fast and customizable. Try this out

[quick-json parser] (https://code.google.com/p/quick-json/) - quick-json features -

  • Compliant with JSON specification (RFC4627)

  • High-Performance JSON parser

  • Supports Flexible/Configurable parsing approach

  • Configurable validation of key/value pairs of any JSON Heirarchy

  • Easy to use # Very Less foot print

  • Raises developer friendly and easy to trace exceptions

  • Pluggable Custom Validation support - Keys/Values can be validated by configuring custom validators as and when encountered

  • Validating and Non-Validating parser support

  • Support for two types of configuration (JSON/XML) for using quick-json validating parser

  • Require JDK 1.5 # No dependency on external libraries

  • Support for Json Generation through object serialization

  • Support for collection type selection during parsing process

For e.g.

JsonParserFactory factory=JsonParserFactory.getInstance();
JSONParser parser=factory.newJsonParser();
Map jsonMap=parser.parseJson(jsonString);
Find elsewhere
🌐
Stack Overflow
stackoverflow.com › questions › 52765774 › not-able-to-print-json-object-in-a-json-array
java - Not able to print json object in a json array - Stack Overflow
So more specifically you'd do something link this (assuming we're not checking for nulls or iterating through the array with a for loop or anything): JSONObject myResponse = new JSONObject(response.toString()); JSONArray jrr= myResponse.getJSONArray("weather"); System.out.println("CITY-"+myResponse.getString("name")); JSONObject weatherObj = jrr.getJSONObject(0); String desc = weatherObj.getString("description"); System.out.println(desc);
🌐
TutorialsPoint
tutorialspoint.com › pretty-print-json-using-org-json-library-in-java
Pretty print JSON using org.json library in Java?\\n
import org.json.JSONObject; import org.json.JSONException; public class PrettyPrintJsonExample { public static void main(String[] args) { // Create a JSON string String jsonString = "{"name":"Ansh", "age":23, "city":"Delhi"}"; // Create a JSONObject object using the JSON string JSONObject jsonObject = new JSONObject(jsonString); // Pretty print the JSON data with an indent factor of 4 spaces String prettyJson = jsonObject.toString(4); // Print the pretty printed JSON data System.out.println(prettyJson); } }
🌐
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
jsonStr = obj.serialize(True) return jsonStr # main part str = createJSONstring() print str · Code 2 - Creating a JSON Formatted String including JSON Array
🌐
TutorialsPoint
tutorialspoint.com › how-can-we-convert-a-jsonarray-to-string-array-in-java
How can we convert a JSONArray to String Array in Java?
import org.json.*; import java.util.*; public class JsonArraytoStringArrayTest { public static void main(String[] args) { JSONArray jsonArray = new JSONArray(); jsonArray.put("INDIA "); jsonArray.put("AUSTRALIA "); jsonArray.put("SOUTH AFRICA "); jsonArray.put("ENGLAND "); jsonArray.put("NEWZEALAND "); List < String > list = new ArrayList < String > (); for (int i = 0; i < jsonArray.length(); i++) { list.add(jsonArray.getString(i)); } System.out.print("JSONArray: " + jsonArray); System.out.print("\n"); String[] stringArray = list.toArray(new String[list.size()]); System.out.print("String Array: "); for (String str: stringArray) { System.out.print(str); } } } JSONArray: ["INDIA ","AUSTRALIA ","SOUTH AFRICA ","ENGLAND ","NEWZEALAND "] String Array: INDIA AUSTRALIA SOUTH AFRICA ENGLAND NEWZEALAND ·
🌐
Javatpoint
javatpoint.com › json-array
JSON Array - javatpoint
JSON Array for beginners and professionals with examples of JSON with java, json array of string, json array of numbers, json array of booleans, json srray of objects, json multidimentional array. Learn JSON array example with object, array, schema, encode, decode, file, date etc.
🌐
YouTube
youtube.com › watch
How to Read and Print Embedded JSON Arrays in Java Using Jackson - YouTube
Discover effective techniques to read and print embedded array values from JSON files using Jackson in Java. Learn how to handle varying lengths of embedded ...
Published   September 22, 2025
Views   1
🌐
Oracle
docs.oracle.com › javaee › 7 › api › javax › json › JsonArray.html
JsonArray (Java(TM) EE 7 Specification APIs)
This is a convenience method for (JsonArray)get(index). ... Returns the number value at the specified position in this array.
🌐
Android Developers
developer.android.com › api reference › jsonarray
JSONArray | 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 · 中文 – 简体
🌐
Stack Overflow
stackoverflow.com › questions › 41771995 › print-array-in-json-using-java-jsonobjectbuilder
Print array in JSON using Java JsonObjectBuilder - Stack Overflow
I am developing a simple project in Java to output a JSON file. The JSON file will include geo-location data which will have to be printed in this exact format: "location" : { "coordinate...