@@@@@@@@@@@@@@@@@@@@@@@@@@@@删除某个对象 @@@@@@@@@@@@@@@@@@@@@@@@@@ public static void main(String[] args) { 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); jsonObject.forEach((key, val) -> { JSONObject obj = (JSONObject) val; JSONArray array = obj.getJSONArray("childrens"); array.clear(); obj.put("childrens",array); }); System.out.println(jsonObject); 结果: {"1":{"childrens":[{"date":"2018-01-01","sex":"man","name":"草根"},{"date":"2017-01-01","sex":"woman","name":"merry"},{"date":"2016-01-01","sex":"woman","name":"liming"}],"sex":"man","name":"maple"}} {"1":{"childrens":[],"sex":"man","name":"maple"}}
@@@@@@@@@@@@@@重新组装对象@@@@@@@@@@@@@@@@@@
public static void main(String[] args) { JSONArray jsonArray = JSONArray.parseArray("[{\"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(jsonArray); jsonArray = jsonArray.stream().map(obj -> { JSONObject returnObj = new JSONObject(); JSONObject jsonObj = (JSONObject)obj; jsonObj.forEach((key,val) -> { returnObj.put(key,((JSONObject)val).getJSONArray("childrens")); }); return returnObj; }).collect(Collectors.toCollection(JSONArray::new)); System.out.println(jsonArray); }
[{"1":{"childrens":[{"date":"2018-01-01","sex":"man","name":"草根"},{"date":"2017-01-01","sex":"woman","name":"merry"},{"date":"2016-01-01","sex":"woman","name":"liming"}],"sex":"man","name":"maple"}}]
[{"1":[{"date":"2018-01-01","sex":"man","name":"草根"},{"date":"2017-01-01","sex":"woman","name":"merry"},{"date":"2016-01-01","sex":"woman","name":"liming"}]}]
@@@@@@@@@@@@@@@过滤并排序@@@@@@@@@@@@@@@@@
public static void main(String[] args) { 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); }
@@@@@@@@@@@@@@@将对象里面的指定的key退出掉@@@@@@@
public static void main(String[] args) { String s = "[{\"category\":\"1\",\"createTime\":1647917852000,\"flag\":\"1\",\"icon\":\"https://djpure-download.djbx.com/life/wx/images/order/survivalFundIcon.png\",\"id\":3,\"provider\":\"1\",\"serviceId\":3,\"serviceName\":\"投连保单\",\"serviceOrder\":\"1\",\"suCode\":\"01\",\"suService\":\"投连服务\",\"updateTime\":1647917858000,\"url\":\"https://life-customer-uat.djbx.com/djbx/dat/index.html#/policyInfoDetail\"},{\"category\":\"1\",\"createTime\":1647917852000,\"flag\":\"1\",\"icon\":\"https://djpure-download.djbx.com/life/wx/images/order/survivalFundIcon.png\",\"id\":1,\"provider\":\"1\",\"serviceId\":1,\"serviceName\":\"投资账户转换\",\"serviceOrder\":\"2\",\"suCode\":\"01\",\"suService\":\"投连服务\",\"updateTime\":1647917858000,\"url\":\"https://life-customer-dev.djbx.com/underwriting/protection/policyList\"}]"; JSONArray jsonArray = JSONArray.parseArray(s); System.out.println(jsonArray); jsonArray = jsonArray.stream().map(obj -> { JSONObject returnObj = new JSONObject(); JSONObject jsonObj = (JSONObject)obj; jsonObj.forEach((key,val) -> { List<String> s2 = Arrays.asList("serviceName","url","serviceId","icon"); if(s2.contains(key)){ returnObj.put(key,val); } }); return returnObj; }).collect(Collectors.toCollection(JSONArray::new)); System.out.println(jsonArray); } }
输出的结果:
[{"icon":"https://djpure-download.djbx.com/life/wx/images/order/survivalFundIcon.png","serviceName":"投连保单","serviceId":3,"url":"https://life-customer-uat.djbx.com/djbx/dat/index.html#/policyInfoDetail"},{"icon":"https://djpure-download.djbx.com/life/wx/images/order/survivalFundIcon.png","serviceName":"投资账户转换","serviceId":1,"url":"https://life-customer-dev.djbx.com/underwriting/protection/policyList"}]