java 去除jsonarray里面jsonarray的重复和合并数据
原来数据如下:
[{"index_id":"19557485","itemid":"70575","time":"1467619020","value":"1"},
{"index_id":"19557442","itemid":"113795","time":"1467619020","value":"1"},
{"index_id":"19557507","itemid":"114227","time":"1467619020","value":"1"},
{"index_id":"19557534","itemid":"114231","time":"1467619020","value":"1"},
{"index_id":"19557534","itemid":"114233","time":"1467619020","value":"1"},
{"index_id":"19557534","itemid":"114237","time":"1467619020","value":"1"},
{"index_id":"19557534","itemid":"114239","time":"1467619020","value":"1"},
{"index_id":"19557593","itemid":"114241","time":"1467619020","value":"1"},
{"index_id":"20118932","itemid":"115778","time":"1467619020","value":"1"},
{"index_id":"11111111","itemid":"222222","time":"1467619020","value":"1"},
{"index_id":"11111111","itemid":"333333","time":"1467619020","value":"1"},
{"index_id":"11111111","itemid":"444444","time":"1467619020","value":"1"},
{"index_id":"11111111","itemid":"555555","time":"1467619020","value":"1"},
{"index_id":"11111111","itemid":"666666","time":"1467619020","value":"1"},
{"index_id":"11111111","itemid":"777777","time":"1467619020","value":"1"},
{"index_id":"19557534","itemid":"1145235","time":"1467619020","value":"1"}]
现在要求合并相同index_id的value值,其实array和list类似,如果遇到list也可用我的方法,新建一个新的arraytemp临时存储json
代码如下:
/**
* 去重复index_id项合并value值
* @param args
*/
public static JSONArray delRepeatIndexid(JSONArray array) {
JSONArray arrayTemp = new JSONArray();
int num = 0;
for(int i = 0;i < array.size();i++){
if(num==0){
arrayTemp.add(array.get(i));
}else{
int numJ = 0;
for(int j = 0;j < arrayTemp.size(); j++){
JSONObject newJsonObjectI = (JSONObject)array.get(i);
JSONObject newJsonObjectJ = (JSONObject)arrayTemp.get(j);
String index_idI = newJsonObjectI.get("index_id").toString();
String valueI = newJsonObjectI.get("value").toString();
String timeI = newJsonObjectI.get("time").toString();
String itemidI = newJsonObjectI.get("itemid").toString();
String index_idJ = newJsonObjectJ.get("index_id").toString();
String valueJ = newJsonObjectJ.get("value").toString();
if(index_idI.equals(index_idJ)){
int newValue = Integer.parseInt(valueI) + Integer.parseInt(valueJ);
arrayTemp.remove(j);
JSONObject newObject = new JSONObject();
newObject.put("index_id", index_idI);
newObject.put("itemid", itemidI);
newObject.put("time", timeI);
newObject.put("value", newValue);
arrayTemp.add(newObject);
break;
}
numJ++;
}
if(numJ-1 == arrayTemp.size()-1){
arrayTemp.add(array.get(i));
}
}
num++;
}
return arrayTemp;
}
输出结果如下:
{"clientip":"10.50.129.11","hostname":"IQSH-D9396","index_gather":[{"hashkey":"","index_id":19557485,"msg_hashkey":"","sourcehost":"","time":1467617940,"type":0,"value":1},{"hashkey":"","index_id":19557442,"msg_hashkey":"","sourcehost":"","time":1467617940,"type":0,"value":1},{"hashkey":"","index_id":19557507,"msg_hashkey":"","sourcehost":"","time":1467617940,"type":0,"value":1},{"hashkey":"","index_id":19557593,"msg_hashkey":"","sourcehost":"","time":1467617940,"type":0,"value":1},{"hashkey":"","index_id":20118932,"msg_hashkey":"","sourcehost":"","time":1467617940,"type":0,"value":1},{"hashkey":"","index_id":11111111,"msg_hashkey":"","sourcehost":"","time":1467617940,"type":0,"value":6},{"hashkey":"","index_id":19557534,"msg_hashkey":"","sourcehost":"","time":1467617940,"type":0,"value":5}]}
java 去除jsonarray里面jsonarray的重复和合并数据相关推荐
- java对jsonarray去重复_java 去除jsonarray里面jsonarray的重复和合并数据
原来数据如下: [{"index_id":"19557485","itemid":"70575","time& ...
- java去重复的集合_如何去除Java中List集合中的重复数据
1.循环list中的所有元素然后删除重复 public class duplicatRemoval { public static List removeDuplicate(List list){ f ...
- Java基础知识强化之集合框架笔记27:ArrayList集合练习之去除ArrayList集合中的重复字符串元素...
1. 去除ArrayList集合中的重复字符串元素(字符串内容相同) 分析: (1)创建集合对象 (2)添加多个字符串元素(包含重复的) (3)创建新的集合 (4)遍历旧集合,获取得到每一个元素 (5 ...
- java object数组转实体类_详解Java中String JSONObject JSONArray List实体类转换
JSON使用阿里的fastJson为依赖包 gradle依赖管理如下: compile group: "com.alibaba", name: "fastjson&quo ...
- java清空json_java – 从JSONArray中删除JSON对象 – Jettison
是否有通过使用索引删除存储在JSONArray中的JSONObject的直接方法.我尝试了所有的可能性.仍然无法从JSON数组中删除JSON对象.任何提示都会有所帮助 谢谢 解决方法: 在java-j ...
- Java 去除重复数据的五种方式
1.使用LinkedHashSet删除arraylist中的重复数据 LinkedHashSet是在一个ArrayList删除重复数据的最佳方法.LinkedHashSet在内部完成两件事: 删除重复 ...
- java json 去除空_详解Java去除json数据中的null空值问题
1.描述 @JsonInclude(JsonInclude.Include.NON_NULL)标记是jackson包提供的json序列化方法,已经集成于Springboot2.0中,此方法的配置意在可 ...
- hutol json null值没了_详解Java去除json数据中的null空值问题
1.描述 @JsonInclude(JsonInclude.Include.NON_NULL)标记是jackson包提供的json序列化方法,已经集成于Springboot2.0中,此方法的配置意在可 ...
- ArrayList去除集合中字符串的重复值
/* * 需求:ArrayList去除集合中字符串的重复值 * * 分析: * 1.创建一个集合对象 * 2.添加多个字符串元素 * 3.创建一个新的集合 * 4.拿旧集合中的元素到新集合中去找 * ...
最新文章
- 74ls390设计任意进制计数器_异步FIFO:设计原理及Verliog源码
- 常用模块-----configparser subprocess
- C++类的构造函数和析构函数
- js利用HTML5的拖拽API做流程图
- 算法入门书籍--2022.04.04
- (自定义组件)通用- X轴横向:溢出滚动 (含代码)- 案例篇
- CSU 1202 剪刀石头布
- 考大学计算机考试作文,机考作文
- Git如何创建本地分支并推送到远程仓库
- Qt——P23 登录窗口布局
- 和shopee哪个好_shopee虾皮哪个站点好,马来还是台湾?马来跟台湾哪个好做?
- BZOJ 2679 [Usaco2012 Open]Balanced Cow Subsets
- 什么是configmap资源、secret资源(实战)
- protoc导出时遇到protoc-gen-go unable to determine Go import path解决方法
- switch语句(C++)
- java itextpdf 5 基础知识
- gridview ItemTemplate下绑定数据
- Speedoffice(excel)如何利用SUMPRODUCT函数求和
- YTU 3386 哈希查找2
- 独立开发并发布自己的一款手游——SpaceWar