json数组去重

时间:2021-03-21 13:35:52
//名字去重
Map<String,Integer> map=new HashMap<String,Integer>();
for(int i=0;i<jows.size();i++){
JSONObject jo = (JSONObject) jows.get(i);
map.put(jo.get("serviceType").toString(),0);
}
for(int i=0;i<jows.size();i++){
JSONObject jo1 = (JSONObject) jows.get(i);
if(map.containsKey(jo1.get("serviceType"))){
map.put(jo1.get("serviceType").toString(),Integer.parseInt(jo1.get("amount").toString())+map.get(jo1.get("serviceType")));
} }
for(Map.Entry<String, Integer> entry : map.entrySet())
{
JSONObject jo2 =new JSONObject();
jo2.put("serviceType", entry.getKey());
jo2.put("amount", entry.getValue());
jows1.add(jo2);
}