程序中的一个服务器,默认返回一些JSONArray。但当一些错误发生时,它返回JSONObject错误的代码。我想解析json来检查错误。下面的代码用来解析错误:
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 ;
}
但是当 jsonResponse 可以运行时确获得 JSONException。它是一个JSONArray,JSONArray
不能转换成 JSONOBject。如何确认 jsonResponse 传过来的值是 JSONArray 还是 JSONObject ?