Possible Duplicate:
Determine whether JSON is a JSONObject or JSONArrayCome verificare se la risposta dal server è JSONAobject o JSONArray?
Ho un server che restituisce un po 'di JSONArray di default, ma quando si verifica un errore che mi restituisce JSONObject con codice di errore. Sto cercando di analizzare JSON e controllare gli errori, ho pezzo di codice che verifica l'errore:
public static boolean checkForError(String jsonResponse) {
boolean status = false;
try {
JSONObject json = new JSONObject(jsonResponse);
if (json instanceof JSONObject) {
if(json.has("code")){
int code = json.optInt("code");
if(code==99){
status = true;
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return status ;
}
ma ottengo JSONException quando jsonResponse è ok ed è un JSONArray (JSONArray non può essere convertito in JsonObject) Come per verificare se jsonResponse mi fornirà JSONArray o JSONObject?