I have this validation for when I receive a JSON String to validate if it is a JSONArray but I think there must be something better out there, im reading an inputStream from a rest service connection, then im gonna check if that String is either a JSONArray or just a JSONObject, after it goes into the validation "charAt(0) == '[' it won't go into the validation cadenaJSON instanceof JSONArray, do you guys know a better way to do this?
while ((cadenaLectura = br.readLine()) != null) {
cadenaJSON = cadenaJSON.concat(cadenaLectura);
}
conn.disconnect();
// If cadenaJSON starts with [ its because its a JSONArray
if (cadenaJSON.charAt(0) == '[') {
// This one isn't working
if ((Object) cadenaJSON instanceof JSONArray) {
System.out.println("Soy un JSONArray");
}
JSONArray jsonArray = new JSONArray(cadenaJSON);
return jsonArray.toString();
} else {
JSONObject json = new JSONObject(cadenaJSON);
return json.toString();
}