Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JSON exception catching

 
Gopu Akraju
Ranch Hand
Posts: 242
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Surround the getString("name") with a try catch statement and in the catch assign null to the array element.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or you could use the JSONObject.has method to check if the key exists first.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic