3

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- when json got value for "patronCheckoutInfo":

{
"ErrorMessage": ""
"Message": "Operation completed successfully"
"Status": "OK"
"Results": {
"LookupPatronInfoResponse": {
"patronAddressInfo": null
"patronCheckoutHistoryInfo": null
"patronCheckoutInfo": [6]
0:  {
"author": "Cobb, Kevin."

Case 2 - when no value:

{
"ErrorMessage": ""
"Message": "Operation completed successfully"
"Status": "OK"
"Results": {
"LookupPatronInfoResponse": {
"patronAddressInfo": null
"patronCheckoutHistoryInfo": null
"patronCheckoutInfo": null
"patronCirculationInfo": null

Tried few tutorials but unable to figure out. Any suggestion is welcome. Thanks in advance.

7
  • 1
    Use optJSONArray(String name) returns value if it exists or null otherwise Commented Mar 17, 2016 at 8:30
  • Try checking like if (!obj2.getString("patronCheckoutInfo") == null)... Because null is not a string to check like that... Commented Mar 17, 2016 at 8:34
  • does it works for you Commented Mar 17, 2016 at 8:36
  • @Raghunandan please convert this into an answer. I'm sure that it will solve OPs question Commented Mar 17, 2016 at 9:20
  • optJSONArray(String name) --> shows null pointer exception Commented Mar 17, 2016 at 9:26

6 Answers 6

3

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 
}
Sign up to request clarification or add additional context in comments.

3 Comments

also you might use has(), but question author have this field in both JSONs
@snachmsm but the document said Returns true if this object has no mapping for name or if it has a mapping whose value is NULL. In this case, both json response had this key, so if in case of the value of second json response had NULL value, it should return true. Right?
This condition is returning - false - even for values in patronCheckoutInfo. Not working.
2

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

Its pretty simple..!! Try this..

boolean response = jsonobject.has("patronCheckoutInfo")

Hope this will help you, Happy coding

2 Comments

it will return true in both cases.
you need to specify the jsonobject that contains the particular key.
2

Try This:

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

Comments

0

Try with

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

to

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

4 Comments

Its empty but still going into the loop. Not working.
patronCheckoutInfo got no values - but condition results in true.
print out the patronCheckoutInfo String and let me know what it gives.
Here you go --> I/patronCheckoutInfo: null
0

Thanks all for the suggestion. Below worked for me:

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

Comments

0

You can try like this:

JSONArray patronCheckoutInfo= obj2.optJSONArray("patronCheckoutInfo");
if(patronCheckoutInfo){
        for (int i = 0; i < invitations.length(); i++) {
              JSONObject patronCheckoutInfoObject= patronCheckoutInfo.getJSONObject(i);
        }
}

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.