list集合根据某一个key的value值相同合并成新的json对象

第一步先把list集合相同的key的value值合并

public static JSONArray subArray(JSONArray array) {
        try {
            JSONArray subArray = new JSONArray();
            JSONObject obj = null;
            JSONObject obj2 = null;
            JSONArray aArray = new JSONArray();
            for (int i = 0, size = array.size(); i < size; i++) {
                System.out.println(array.getJSONObject(i));
                obj = array.getJSONObject(i);
                if (i + 1 < size) {
                    if (i - 1 < 0) {
                        subArray = new JSONArray();
                        subArray.add(obj);
                    } else {
                        obj2 = array.getJSONObject(i - 1);
                        if (obj.getString("shopname").equals(obj2.getString("shopname"))) {
                            subArray.add(obj);
                        } else {
                            aArray.add(subArray);
                            subArray = new JSONArray();
                            subArray.add(obj);
                        }
 
                    }
                } else {
                    if (size + 1 > 0) {
                        obj2 = array.getJSONObject(i - 1);
                        if (obj.getString("shopname").equals(obj2.getString("shopname"))) {
                            subArray.add(obj);
                            if (size - 1 == i) {
                                aArray.add(subArray);
                            }
                        } else {
                            aArray.add(subArray);
                            subArray = new JSONArray();
                            subArray.add(obj);
                            if (i + 1 == size) {
                                aArray.add(subArray);
                            }
                        }
 
                    } else {
                        subArray.add(obj);
                        aArray.add(subArray);
                    }
 
                }
 
            }
            return aArray;
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }
    }

将返回的jsonarray数组获取到你需要的数据重新封装

JSONArray jsonArray = this.subArray(js);
            
            List> list2 = new ArrayList>();
            
            for (int i = 0; i < jsonArray.size(); i++) {
                
                Map map = new HashMap();
                
                JSONArray json=JSONArray.fromObject(jsonArray.get(i));
                
                JSONObject fromObject = JSONObject.fromObject(json.get(0));
                
                String shopname = fromObject.getString("shopname");
                
                map.put(shopname, jsonArray.get(i).toString());
                
                list2.add(map);
            }
            String jsonStr = JSONArray.fromObject(list2).toString();
            JSONArray jsonArray2 = JSONArray.fromObject(jsonStr);
            pageOut(response, (jsonArray2 + ""));

你可能感兴趣的:(list集合根据某一个key的value值相同合并成新的json对象)