Check if jsonArray is not [[]]

303 Views Asked by At

I have this weird json Array

[
     [
     ]
]

How can I check whether this json arary is in this form or not? I tried to check whether jsonArray.length() > 0 but for [[]] this is giving me true but I expect to be false.

  • How can I fix?
2

There are 2 best solutions below

2
On

jsonArray will have a size of one as it has an empty array inside it. So, you can use this condition to check for nested empty array [ [ ] ].

(jsonArray.size()==1 && jsonArray[0].size()==0)
0
On

try this: (jsonArray != null && jsonArray.length() == 1 && jsonArray.optJSONArray(0) != null && jsonArray.getJSONArray(0).length() == 0)