我有一组从服务器接收并操作的 JSONObject
值。大多数时候我得到一个带有值的 JSONObject
(比如说统计数据),有时,它返回一个带有代码和错误描述的 Error
对象。
我如何构造我的代码,以便它在返回错误时不会中断。我以为我可以做到这一点,但不起作用。
public void processResult(JSONObject result) {
try {
if(result.getJSONObject(ERROR) != null ){
JSONObject error = result.getJSONObject(ERROR);
String error_detail = error.getString(DESCRIPTION);
if(!error_detail.equals(null)) {
//show error login here
}
finish();
}
else {
JSONObject info = result.getJSONObject(STATISTICS);
String stats = info.getString("production Stats"));
}
}
}
最佳答案
使用 .has(String)
和 .isNull(String)
保守的用法可能是;
if (record.has("my_object_name") && !record.isNull("my_object_name")) {
// Do something with object.
}
https://stackoverflow.com/questions/12585492/