This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach 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 Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "JSON exception catching" Watch "JSON exception catching" New topic
Author

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
Rancher

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


Joanne
 
 
subject: JSON exception catching
 
Similar Threads
How to connect to the internet behind proxy ?
Struts and JSON
problem in reading input stream from a socket
How can i pass the JSONObject to the servlet