Remove all elements except one from JSONArray of JSONObjects
问题 I have a JSONArray (org.json) like this: [ { "a": "a" }, { "b": "a" }, { "c": "a" }, { "d": "a" }, { "e": "a" }, { "f": "a" }, { "g": "a" } ] I would like to remove all the JSONObject s that do not have the key a . Is there a better way to do other than my naive approach? Iterator objects = jsonArray.iterator(); while (objects.hasNext()) { Object o = objects.next(); if (o instanceof JSONObject && !((JSONObject) o).has("a")) { objects.remove(); } } Expected output: [{"a": "a"}] 回答1: If you're