JSONObject o1 = new JSONObject(); o1.put("key", "2"); o1.put("key2", "3"); JSONObject o2 = new JSONObject(); o2.put("key", "1"); o2.put("key3", "3"); JSONArray ja = new JSONArray(); ja.add(o1); ja.add(o2); // Iterator<Object> iterator = ja.stream().iterator(); // Stream<Object> objectStream = ja.stream().filter(s -> "1".equals(((JSONObject) s).get("key"))); boolean key = ja.removeIf(s -> "1".equals(((JSONObject) s).get("key"))); System.out.println(key); for(int i =0; i<ja.size();i++) { JSONObject jsonObject = ja.getJSONObject(i); Set<Map.Entry<String, Object>> entries = jsonObject.entrySet(); for (Map.Entry<String, Object> str : entries) { System.out.println(str.getValue()); } } }
//关键字:removeIf 删除指定的key // boolean key = ja.removeIf(s -> "1".equals(((JSONObject) s).get("key"))); // for(int i =0; i<ja.size();i++) { // JSONObject jsonObject = ja.getJSONObject(i); // Set<Map.Entry<String, Object>> entries = jsonObject.entrySet(); // for (Map.Entry<String, Object> str : entries) { // System.out.println(str.getValue()); // } // } //获取第一个findFirst //获取最后一个findLast // JSONArray jsonArray = JSONArray.parseArray("[{\"name\":\"草根\",\"sex\":\"man\",\"date\":\"2018-01-01\"},{\"name\":\"merry\",\"sex\":\"woman\",\"date\":\"2017-01-01\"},{\"name\":\"liming\",\"sex\":\"woman\",\"date\":\"2016-01-01\"}]"); // JSONObject jsonObject = (JSONObject)jsonArray.stream().sorted((a,b) -> -1).findFirst().get(); // System.out.println(jsonObject); //清空json对象中的某个数组 // JSONObject jsonObject = JSONObject.parseObject("{\"1\": {\"name\":\"maple\",\"sex\":\"man\",\"childrens\":[{\"name\":\"草根\",\"sex\":\"man\",\"date\":\"2018-01-01\"},{\"name\":\"merry\",\"sex\":\"woman\",\"date\":\"2017-01-01\"},{\"name\":\"liming\",\"sex\":\"woman\",\"date\":\"2016-01-01\"}]}}"); // jsonObject.forEach((key, val) -> { // JSONObject obj = (JSONObject) val; // JSONArray array = obj.getJSONArray("childrens"); // array.clear(); // obj.put("childrens",array); // }); // System.out.println(jsonObject); //过滤数据并按时间字符串排序 JSONObject jsonObject = JSONObject.parseObject("{\"1\": {\"name\":\"maple\",\"sex\":\"man\",\"childrens\":[{\"name\":\"草根\",\"sex\":\"man\",\"date\":\"2018-01-01\"},{\"name\":\"merry\",\"sex\":\"woman\",\"date\":\"2017-01-01\"},{\"name\":\"liming\",\"sex\":\"woman\",\"date\":\"2016-01-01\"}]}}"); System.out.println(jsonObject); DateFormat df = new SimpleDateFormat("yyyy-MM-dd"); Comparator<Object> dateComparator = (a, b) -> { int result = 0; try { Date dt1 = df.parse(((JSONObject)a).getString("date")); Date dt2 = df.parse(((JSONObject)b).getString("date")); result = dt1.compareTo(dt2); } catch (Exception ex) { ex.printStackTrace(); } finally { return result; } }; jsonObject.forEach((key, val) -> { JSONObject obj = (JSONObject) val; if (obj.getJSONArray("childrens") != null) { JSONArray array = obj.getJSONArray("childrens"); array = array.stream().filter(arrObj -> !"merry".equals(((JSONObject) arrObj).getString("name"))) .sorted(dateComparator) .collect(Collectors.toCollection(JSONArray::new)); obj.put("childrens", array); } else { obj.put("childrens", new JSONArray()); } }); System.out.println(jsonObject);