The moose likes Beginning Java and the fly likes JSON exception catching Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "JSON exception catching" Watch "JSON exception catching" New topic

JSON exception catching

Gopu Akraju
Ranch Hand

Joined: Jan 13, 2008
Posts: 242
Hi I am trying to catch exception for missing JSON objects

sometines "names" may not be found. Exception is being printed as org.json.JSONException: JSONObject["name"] not found.

But I want to add a "null" in the aaray list "a" and move on to next item in the for loop. How do I implement it. Thanks.>
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

Surround the getString("name") with a try catch statement and in the catch assign null to the array element.

"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Joanne Neal

Joined: Aug 05, 2005
Posts: 3742
Or you could use the JSONObject.has method to check if the key exists first.

I agree. Here's the link:
subject: JSON exception catching
It's not a secret anymore!