I have a set of JSONObject values which i receive from a server and operate on. Most times I get a JSONObject with a value (let's say statistics) and sometimes, it returns an Error object with a code and a description of the error.
JSONObject
Error
How do I structure my code so that it doesn't break if it returns the error. I thought I could do this, but doesn't work.
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")); } } }
Use .has(String) and .isNull(String)
.has(String)
.isNull(String)
A conservative usage could be;
if (record.has("my_object_name") && !record.isNull("my_object_name")) { // Do something with object. }
1.4m articles
1.4m replys
5 comments
57.0k users