-1

my json string is

String RESPONSE = "  { "Table": [] }  ";

and I use

 JSONObject jsonObj = new JSONObject(RESPONSE);
 JSONArray contacts = jsonObj.getJSONArray("Table");

Therefore, contacts = [] I mean empty. How can I control that array is empty. After this controller I use this command

JSONObject c = contacts.getJSONObject(0);

Of course is not empty :)

Sefa Taşcan
  • 57
  • 2
  • 6

2 Answers2

1

You can use length function:

JSONObject jsonObj = new JSONObject(RESPONSE);
JSONArray contacts = jsonObj.getJSONArray("Table");
if(contacts.length() > 0 ) {
    JSONObject c = contacts.getJSONObject(0);
}
Veneet Reddy
  • 2,707
  • 1
  • 24
  • 40
1

You could use the isNull() function.

JSONObject jsonObj = new JSONObject(RESPONSE);
JSONArray contacts = jsonObj.getJSONArray("Table");
if(!contacts.isNull(0)) {
   JSONObject c = contacts.getJSONObject(0);
}
Dec
  • 203
  • 2
  • 11