1

I have to parse a json whom a field can be empty:

{"fullField":"ok","canBeEmpty":""}

if I try to parse this string overall parsing fails with a "no value for canBeEmpty". For each json item I execute:

json_data.getString("field"); //throws exception if empty

I'd like to still keep the parsing, setting the canBeEmpty value to a default string...is it possibile?

3
  • Are you sure that getString is throwing an exception? It should only throw an exception if the field does not exist, not if the field value is empty - which is a perfectly valid value for a string. I think you're problem is elsewhere. Please post the relevant part of your parser. I also don't see what "field" has to do with the example you've shown. Commented Nov 11, 2012 at 11:19
  • Ok, checking better I see that some field have "null" as value..I'm using isNull and everything's fine! @imrankhan if you want post as answer and I'll give you "correct" ;) Commented Nov 11, 2012 at 11:33
  • @Phate : Thanks Phate,you can check my answer !!! :) Commented Nov 11, 2012 at 11:37

1 Answer 1

4

you can use JSONObject.JSONObject(String name) to check if any name is exist or not in json object as:

if(JSONObject.isNull("field")){
// do something here
}
else{
//do something here
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.