There is no more simple and secure solution than using an iterator.
JSONObject names () method returns a JSONArray of the JSONObject keys, so you can simply walk though it in loop:
JSONObject objects = new JSONObject ();
JSONArray key = objects.names ();
for (int i = 0; i < key.length (); ++i) {
String keys = key.getString (i);
String value = objects.getString (keys);
}