Here you get JSONObject so change this line:

JSONArray jsonArray = new JSONArray(readlocationFeed); 

with following:

JSONObject jsnobject = new JSONObject(readlocationFeed);

and after

JSONArray jsonArray = jsnobject.getJSONArray("locations");
for (int i = 0; i < jsonArray.length(); i++) {
    JSONObject explrObject = jsonArray.getJSONObject(i);
}
Answer from kyogs on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › java › how-to-convert-json-array-to-string-array-in-java
How to Convert JSON Array to String Array in Java? - GeeksforGeeks
July 23, 2025 - // importing the packages import java.util.*; import org.json.*; public class GFG { public static void main(String[] args) { // Initialising a JSON example array JSONArray exampleArray = new JSONArray(); // Entering the data into the array ...
🌐
Blogger
javarevisited.blogspot.com › 2013 › 04 › convert-json-array-to-string-array-list-java-from.html
How to Convert JSON array to String array in Java - GSon example
Java programming tutorial to convert JSON array to String array with example, by using Gson library. Though you can also use other open source library, Gson seems easy to me.
🌐
Stleary
stleary.github.io › JSON-java › org › json › JSONArray.html
JSONArray
If the value is not a string and is not null, then it is converted to a string. ... A String value. public String optString(int index, String defaultValue) Get the optional string associated with an index. The defaultValue is returned if the key is not found. ... A String value. ... Append a boolean value. This increases the array's length by one. ... Put a value in the JSONArray, where the value will be a JSONArray which is produced from a Collection.
Top answer
1 of 3
47

To have a string value inside your JSON array, you must remember to backslash escape your double-quotes in your Java program. See the declaration of s below.

String s = "[[\"110917       \", 3.0099999999999998, -0.72999999999999998, 2.8500000000000001, 2.96, 685.0, 38603.0], [\"110917    \", 2.71, 0.20999999999999999, 2.8199999999999998, 2.8999999999999999, 2987.0, 33762.0]]";

Your code in the main() method works fine. Below is just a minor modification of your code in the main() method.

System.out.println("String to Json Array Stmt");
JsonParser parser = new JsonParser();
JsonElement tradeElement = parser.parse(s);
JsonArray trade = tradeElement.getAsJsonArray();
System.out.println(trade);

Lastly, remember to prefix your statement "com.google.gson.*" with the keyword "import", as shown below.

import com.google.gson.*;
2 of 3
11

I don't see the problem. This code runs fine for me:

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;


public class GsonExample {
    public static void main(String[] args) {
        String s= "[[\"110917\", 3.0099999999999998, -0.72999999999999998," +
                "2.8500000000000001, 2.96, 685.0, 38603.0], [\"110917\", 2.71," +
                "0.20999999999999999, 2.8199999999999998, 2.8999999999999999," +
                "2987.0, 33762.0]]";


        JsonParser  parser = new JsonParser();
        JsonElement elem   = parser.parse( s );

        JsonArray elemArr = elem.getAsJsonArray();
        System.out.println( elemArr );
    }
}

The only problem maybe is that you failed to properly escape the double quotes in your s string literal.

Find elsewhere
🌐
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?
June 6, 2025 - We can convert a JSONArray to String Array by using a simple loop as shown in the example below - 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); } } }
🌐
Google Groups
groups.google.com › g › vertx › c › 5fgz-e2rTbk
casting a JsonArray to a specific type
Also, if you're using Java 8 you can make the List to List<String> conversion a little prettier with the stream API. Something like... List<String> list = array.toList().stream().collect(Collectors.mapping(value -> value.toString()), Collectors::toList); Might have that a little wrong since I typed that from my phone :-) On Dec 28, 2014, at 3:23 PM, Alexander Lehmann <alex...@gmail.com> wrote: I wonder if there is a smarter way to cast a JsonArray to a List of specific type.
🌐
Java Code Geeks
javacodegeeks.com › home › core java
Converting a JSON Object to a JSON Array in Java - Java Code Geeks
August 1, 2025 - Determine whether the result should be an array of objects, an array of values, or a custom structure · Be mindful of element ordering, as JSON objects do not guarantee key order · You can use the org.json library, which offers straightforward APIs for working with JSON data in Java. Let’s explore its usage through a practical example. // JsonConversionExample.java import org.json.JSONArray; import org.json.JSONObject; public class JsonConversionExample { public static void main(String[] args) { String jsonString = "{ \"apple\": 1, \"banana\": 2, \"cherry\": 3 }"; JSONObject jsonObject = new JSONObject(jsonString); JSONArray jsonArray = new JSONArray(jsonObject.toMap().values()); System.out.println("Converted JSON Array:"); System.out.println(jsonArray.toString(2)); } }
🌐
Oracle
docs.oracle.com › javaee › 7 › api › javax › json › JsonArray.html
JsonArray (Java(TM) EE 7 Specification APIs)
JsonArray provides various accessor methods to access the values in an array. The following example shows how to obtain the home phone number "212 555-1234" from the array built in the previous example: JsonObject home = array.getJsonObject(0); String number = home.getString("number");
🌐
TutorialsPoint
tutorialspoint.com › how-to-convert-java-array-collection-to-json-array
How to convert Java Array/Collection to JSON array?
July 30, 2019 - import org.json.JSONArray; public class ArrayToJson { public static void main(String args[]) { String [] myArray = {"JavaFX", "HBase", "JOGL", "WebGL"}; JSONArray jsArray = new JSONArray(); for (int i = 0; i < myArray.length; i++) { jsArray.put(myArray[i]); } System.out.println(jsArray); } }
Top answer
1 of 3
2

There are actually two libraries containing the similar JSONArray class which makes it confusing somehow:

<!-- https://mvnrepository.com/artifact/com.googlecode.json-simple/json-simple -->
<dependency>
    <groupId>com.googlecode.json-simple</groupId>
    <artifactId>json-simple</artifactId>
    <version>1.1.1</version>
</dependency>

and

<!-- https://mvnrepository.com/artifact/org.json/json -->
<dependency>
    <groupId>org.json</groupId>
    <artifactId>json</artifactId>
    <version>20180813</version>
</dependency>

The org.json library has a JSONArray class which has a constructor accepting a String that can be parsed to a JSONArray. This is working when you have some simple form of Json array string like this:

["One", "two"]

So you can parse it simply like this to achieve the JSONArray:

import org.json.JSONArray;

public class Test {
    public static void main(String[] args) {
        String val = "[\"One\", \"Two\"]";

        JSONArray jsonArr = new JSONArray(val);  
        for (int i = 0; i < jsonArr.length(); i++) {
            System.out.println( jsonArr.getString( i ) );
        }
    }
}

But this doesn't work simply for more complex Json strings similar to yours which is containing Json objects with multiple properties.

In this case you may find the other library more helpful. It has a JSONParser class that can parse complex Json objects within the strings:

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

public class JSONArrayTest {
    public static void main(String[] args) throws ParseException {
        String val = "[{\"name\":\"mobilenumber\",\"value\":\"9010000000\"},{\"name\":\"amount\",\"value\":\"200\"},{\"name\":\"ccffee\",\"value\":\"0\"},{\"name\":\"totalfee\",\"value\":\"200\"}]";

        JSONArray jsonArr = (JSONArray) new JSONParser().parse( val );

        for (int i = 0; i < jsonArr.size(); i++) {
            JSONObject jsonObj = (JSONObject) jsonArr.get( i );
            System.out.println( jsonObj );
        }
    }
}

Hope this would be helpful.

2 of 3
1

You could try

    JSONParser parse = new JSONParser();
    org.json.simple.JSONObject jobj=(org.json.simple.JSONObject)parse.parse(your String);

Iterator obj = jobj.keys();
JSONArray jsonArray = new JSONArray();

while (obj.hasNext()){
    String key = (String) obj.next();
    jsonArray.put(obj.get(key));
}
🌐
Processing
processing.github.io › processing-javadocs › core › processing › data › JSONArray.html
JSONArray
The internal form is an object ... or the JSONObject.NULL object. The constructor can convert a JSON text into a Java object. The toString method converts to JSON text....