When trying to parse the following JSON string using the json.simple library:
[
{"id" : "6d7662a9.f8ba04"},
{"id" : "2da98cc2.145ba4"},
{"id" : "45492640.a17d68"}
]
I get this exception:
java.lang.ClassCastException: org.json.simple.JSONArray cannot be cast to org.json.simple.JSONArray
This is how I'm doing it:
JSONArray json = (JSONArray) new JSONParser().parse(jsonString);
The JSON string is an array so not sure why that exception is thrown.
There are several similar questions here but in their cases, they were trying to cast a JSONObject
to a JSONArray
so it makes sense that an exception is thrown but in this case, it looks correct.
-----------------EDITS-----------------
I added a line to print the class of the object, like this:
Object json = new JSONParser().parse(jsonString);
System.out.println(json.getClass());
That prints the following line:
class org.json.simple.JSONArray
and in the next line, I have this if:
if(json instanceof JSONArray) {
System.out.println("This is a JSONArray");
}
But it does not access the if, so it is really weird, because first I check that the object is a JSONArray but it does not print "This is a JSONArray"