我自个磨出来的,难受

JSONArray jsonarray = new JSONArray();
Set<Object> jsonObjects = new HashSet<>();
for (Object obj : jsonarray) {
	JSONObject jsonObject = JSONObject.parseObject(JSONObject.toJSONString(obj));
	if (jsonObject.containsKey("key")) {
	    jsonObjects.add(jsonObject);
    }
}
jsonarray.removeAll(jsonObjects);

来自于FeelTouch,厉害

JSONObject o1 = new JSONObject();
o1.put("key", 324);

JSONObject o2 = new JSONObject();
o2.put("key", 325);

JSONObject o3 = new JSONObject();
o3.put("key", 325);

JSONObject o4 = new JSONObject();
o4.put("key", 327);

JSONArray ja =  new JSONArray();
ja.add(o1);
ja.add(o2);
ja.add(o3);
ja.add(o4);

Iterator<Object> o = ja.iterator();
while (o.hasNext()) {
    JSONObject jo = (JSONObject) o.next();
    if(jo.getIntValue("key") == 325) {
        //ja.remove(jo); //不要用这种方式删除,会报出ConcurrentModificationException
        o.remove(); //这种方式OK的
    }
}
System.out.println(ja);
Logo

有“AI”的1024 = 2048,欢迎大家加入2048 AI社区

更多推荐