Skip to content Skip to sidebar Skip to footer

How To Check If Json Array Has Any Value?

I am using the condition - if (!obj2.getString('patronCheckoutInfo').equals('null')) { But it does not work for the case when array got the value. Eg: Case1-

Solution 1:

You can use support function isNull (http://developer.android.com/reference/org/json/JSONObject.html#isNull(java.lang.String))

So, re-write your code in this case:

if (!obj2.isNull("patronCheckoutInfo")) {
//do something 
}

Solution 2:

Yes, you can check if the particular key present in the Json.

Its pretty simple..!! Try this..

booleanresponse= jsonobject.has("patronCheckoutInfo")

Hope this will help you, Happy coding

Solution 3:

Try This:

if(obj2.has("patronCheckoutInfo")){
//write your code
}

Solution 4:

Try with

if (!obj2.getString("patronCheckoutInfo").equals("null"))

to

StringpatronCheckoutInfo= obj2.getString("patronCheckoutInfo");
if(!TextUtils.isEmpty(patronCheckoutInfo)){
  // string has value
}else{
  // string is empty
}

Solution 5:

Thanks all for the suggestion. Below worked for me:

if (obj2.getString("patronCheckoutInfo") != null)

Post a Comment for "How To Check If Json Array Has Any Value?"