0

This is my JSON response how can I check for the null array in android

[
 {
  "OrderDetails":[
     {
       "userName":"akhilm"
       "acc_name":"akhil001"
     }
    ],
  "OrderAttributes":null
  "OrderServices":[
     {
       "name":"aditi"
       "id":78934
     }
    ],
  "Message":null
  "Description":null
 }
]

The JSON data is dynamic

7
  • 4
    Invalid JSON Commented Oct 23, 2017 at 5:44
  • Using JSONObject.isNull(<ARRAY_KEY>) ? Commented Oct 23, 2017 at 5:45
  • Y is it invalid JSON, the IOS team made the app already with this JSON Commented Oct 23, 2017 at 5:46
  • i am new to android, so i couldn't figure it out Commented Oct 23, 2017 at 5:46
  • 3
    @AkhilMankala: If IOS team made the app, then they were did wrong development. First json should be valid then it will be used in app Commented Oct 23, 2017 at 5:51

3 Answers 3

3
try {
    JSONObject json = new JSONObject(jsonString);

    if(json.isNull("OrderAttributes")) {
        //TODO: it is null
    } else {
        //TODO: it is not null
        String orderAttributes = json.getString("OrderAttributes");
    }
} catch (JSONException e) {
    e.printStackTrace();
}
Sign up to request clarification or add additional context in comments.

Comments

0
if (json.has("key string")) {
    String status = json.getString("key string"));
 }
else {
 Log.e("status", "key value not present ")
}

1 Comment

Please edit with more information. Code-only and "try this" answers are discouraged, because they contain no searchable content, and don't explain why someone should "try this".
0

Try like this:

JSONObject jsonObj = new JSONObject(jsonString);        
    System.out.println(jsonObj .isNull("error") ? " error is null ":" error is not null" );

Hope it helps you.

Comments