json Stream 探索

@@@@@@@@@@@@@@@@@@@@@@@@@@@@删除某个对象 @@@@@@@@@@@@@@@@@@@@@@@@@@
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"}]

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值