This week's book giveaway is in the Big Data forum.
We're giving away four copies of Elasticsearch in Action and have Radu Gheorghe & Matthew Lee Hinman on-line!
See this thread for details.
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

Win a copy of Elasticsearch in Action this week in the Big Data forum!
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.

With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
subject: JSON exception catching