Java 如何避免向Jsonobject添加重复条目
我在Elasticsearch数据库中有以下数据集Java 如何避免向Jsonobject添加重复条目,java,arrays,json,list,Java,Arrays,Json,List,我在Elasticsearch数据库中有以下数据集 "hits": [ { "_index": "autotopology", "_type": "topologydata", "_id": "AWYum6htsO8xYxsN28QK", "_score": 0.47000363, "_source": { "host": "192.168.0.223", "os"
"hits": [
{
"_index": "autotopology",
"_type": "topologydata",
"_id": "AWYum6htsO8xYxsN28QK",
"_score": 0.47000363,
"_source": {
"host": "192.168.0.223",
"os": "Linux",
"services": [
{
"port": "80",
"protocol": "tcp",
"status": "open",
"service": "http"
},
{
"port": "2181",
"protocol": "tcp",
"status": "open",
"service": "eforward"
},
{
"port": "3000",
"protocol": "tcp",
"status": "open",
"service": "ppp"
},
{
"port": "3306",
"protocol": "tcp",
"status": "open",
"service": "mysql"
},
{
"port": "3340",
"protocol": "tcp",
"status": "open",
"service": "anet-m"
},
{
"port": "4567",
"protocol": "tcp",
"status": "open",
"service": "tram"
},
{
"port": "5601",
"protocol": "tcp",
"status": "open",
"service": "esmagent"
},
{
"port": "5665",
"protocol": "tcp",
"status": "open",
"service": "unknown"
},
{
"port": "5800",
"protocol": "tcp",
"status": "open",
"service": "vnc-http"
},
{
"port": "5900",
"protocol": "tcp",
"status": "open",
"service": "vnc"
},
{
"port": "6556",
"protocol": "tcp",
"status": "open",
"service": "unknown"
},
{
"port": "8009",
"protocol": "tcp",
"status": "open",
"service": "ajp13"
},
{
"port": "8080",
"protocol": "tcp",
"status": "open",
"service": "http-proxy"
},
{
"port": "8888",
"protocol": "tcp",
"status": "open",
"service": "sun-answerbook"
},
{
"port": "9200",
"protocol": "tcp",
"status": "open",
"service": "wap-wsp"
},
{
"port": "9300",
"protocol": "tcp",
"status": "open",
"service": "vrace"
},
{
"port": "10514",
"protocol": "tcp",
"status": "open",
"service": "unknown"
},
{
"port": "24224",
"protocol": "tcp",
"status": "open",
"service": "unknown"
},
{
"port": "27017",
"protocol": "tcp",
"status": "open",
"service": "mongod"
},
{
"port": "33556",
"protocol": "tcp",
"status": "open",
"service": "unknown"
}
],
"time": 1538380216803,
"customerID": "customer1234560000"
}
},
{
"_index": "autotopology",
"_type": "topologydata",
"_id": "AWY-hRMpVsmS0VCKavLL",
"_score": 0.47000363,
"_source": {
"host": "192.168.0.223",
"os": "Linux",
"services": [
{
"port": "80",
"protocol": "tcp",
"status": "open",
"service": "http"
},
{
"port": "2181",
"protocol": "tcp",
"status": "open",
"service": "eforward"
},
{
"port": "2393",
"protocol": "tcp",
"status": "open",
"service": "ms-olap1"
},
{
"port": "3000",
"protocol": "tcp",
"status": "open",
"service": "ppp"
},
{
"port": "3306",
"protocol": "tcp",
"status": "open",
"service": "mysql"
},
{
"port": "4567",
"protocol": "tcp",
"status": "open",
"service": "tram"
},
{
"port": "5601",
"protocol": "tcp",
"status": "open",
"service": "esmagent"
},
{
"port": "5665",
"protocol": "tcp",
"status": "open",
"service": "unknown"
},
{
"port": "5800",
"protocol": "tcp",
"status": "open",
"service": "vnc-http"
},
{
"port": "5900",
"protocol": "tcp",
"status": "open",
"service": "vnc"
},
{
"port": "6556",
"protocol": "tcp",
"status": "open",
"service": "unknown"
},
{
"port": "8009",
"protocol": "tcp",
"status": "open",
"service": "ajp13"
},
{
"port": "8080",
"protocol": "tcp",
"status": "open",
"service": "http-proxy"
},
{
"port": "8888",
"protocol": "tcp",
"status": "open",
"service": "sun-answerbook"
},
{
"port": "9200",
"protocol": "tcp",
"status": "open",
"service": "wap-wsp"
},
{
"port": "9300",
"protocol": "tcp",
"status": "open",
"service": "vrace"
},
{
"port": "10514",
"protocol": "tcp",
"status": "open",
"service": "unknown"
},
{
"port": "24224",
"protocol": "tcp",
"status": "open",
"service": "unknown"
},
{
"port": "27017",
"protocol": "tcp",
"status": "open",
"service": "mongod"
},
{
"port": "33556",
"protocol": "tcp",
"status": "open",
"service": "unknown"
}
]
正如您在这两个文档中看到的,大多数值都是重复的,当我获取和解析这些数据时,我必须跳过向jsonobject添加重复的服务
到目前为止,我已经做了以下事情
List<JSONObject> serviceNodeList = new ArrayList<JSONObject>();
JSONObject jsonObj = new JSONObject("read json data);
JSONArray array = jsonObj.getJSONObject("hits").getJSONArray("hits");
for (int i = 0; i < array.length(); i++) {
JSONObject innerObj = array.getJSONObject(i);
String host = innerObj.getJSONObject("_source").getString("host");
JSONArray serviceList = innerObj.getJSONObject("_source").getJSONArray("services");
for (int j = 0; j < serviceList.length(); j++) {
JSONObject serviceObject2 = new JSONObject();
JSONObject serviceObj = serviceList.getJSONObject(j);
String service = (String) serviceObj.get("service");
String port = serviceObj.getString("port");
String protocol = serviceObj.getString("protocol");
System.out.println(serviceObj.get("service"));
System.out.println("-----------");
list.add(obj);
serviceObject2.put("shape", "image");
serviceObject2.put("label", service);
File f = new File("images/" + service + ".png");
serviceObject2.put("image", "images/Circle-icon.png");
serviceObject2.put("widthMin", 15);
serviceObject2.put("widthMax", 15);
serviceObject2.put("x", 100);
serviceObject2.put("y", 100);
serviceObject2.put("title", "Server/Host: " + host + "\n" + "Port :" + port);
serviceObject2.put("value", 1);
serviceObject2.put("port", port);
serviceObject2.put("protocol", protocol);
serviceNodeList.add(serviceObject2);
}
}
List serviceNodeList=new ArrayList();
JSONObject jsonObj=新的JSONObject(“读取json数据”);
JSONArray数组=jsonObj.getJSONObject(“hits”).getJSONArray(“hits”);
对于(int i=0;i
在这里,我不想向jsonObject添加重复的服务。如果我理解正确,您不想添加下面这样的服务,因为这些服务对于每个主机都是重复的
{
"port": "80",
"protocol": "tcp",
"status": "open",
"service": "http"
}
您可以获取java.util.HashSet,并在集合中不断添加密钥(比如“http”)。对于您读取的每个服务,您可以将检查
List<JSONObject> serviceNodeList = new ArrayList<JSONObject>();
JSONObject jsonObj = new JSONObject("read json data);
JSONArray array = jsonObj.getJSONObject("hits").getJSONArray("hits");
Set<String> keys = new HashSet<String>(); //Create hash set
for (int i = 0; i < array.length(); i++) {
JSONObject innerObj = array.getJSONObject(i);
String host = innerObj.getJSONObject("_source").getString("host");
JSONArray serviceList = innerObj.getJSONObject("_source").getJSONArray("services");
for (int j = 0; j < serviceList.length(); j++) {
JSONObject serviceObj = serviceList.getJSONObject(j);
String service = (String) serviceObj.get("service");
String port = serviceObj.getString("port");
String protocol = serviceObj.getString("protocol");
if(!keys.contains(service)){ //Check key for existance in set so that you avoid duplicates service. You should create set key based on how you want to avoid duplicate
JSONObject serviceObject2 = new JSONObject();
System.out.println(serviceObj.get("service"));
System.out.println("-----------");
list.add(obj);
serviceObject2.put("shape", "image");
serviceObject2.put("label", service);
File f = new File("images/" + service + ".png");
serviceObject2.put("image", "images/Circle-icon.png");
serviceObject2.put("widthMin", 15);
serviceObject2.put("widthMax", 15);
serviceObject2.put("x", 100);
serviceObject2.put("y", 100);
serviceObject2.put("title", "Server/Host: " + host + "\n" + "Port :" + port);
serviceObject2.put("value", 1);
serviceObject2.put("port", port);
serviceObject2.put("protocol", protocol);
serviceNodeList.add(serviceObject2);
keys.add(service); //Add key to set
}
}
}
List serviceNodeList=new ArrayList();
JSONObject jsonObj=新的JSONObject(“读取json数据”);
JSONArray数组=jsonObj.getJSONObject(“hits”).getJSONArray(“hits”);
Set keys=new HashSet()//创建哈希集
对于(int i=0;i
如果我理解正确,您不想添加下面这样的服务,因为这些服务对于每个主机都是重复的
{
"port": "80",
"protocol": "tcp",
"status": "open",
"service": "http"
}
您可以使用java.util.HashSet,并在集合中不断添加键(比如“http”)。对于您阅读的每一项服务,您可以在
List<JSONObject> serviceNodeList = new ArrayList<JSONObject>();
JSONObject jsonObj = new JSONObject("read json data);
JSONArray array = jsonObj.getJSONObject("hits").getJSONArray("hits");
Set<String> keys = new HashSet<String>(); //Create hash set
for (int i = 0; i < array.length(); i++) {
JSONObject innerObj = array.getJSONObject(i);
String host = innerObj.getJSONObject("_source").getString("host");
JSONArray serviceList = innerObj.getJSONObject("_source").getJSONArray("services");
for (int j = 0; j < serviceList.length(); j++) {
JSONObject serviceObj = serviceList.getJSONObject(j);
String service = (String) serviceObj.get("service");
String port = serviceObj.getString("port");
String protocol = serviceObj.getString("protocol");
if(!keys.contains(service)){ //Check key for existance in set so that you avoid duplicates service. You should create set key based on how you want to avoid duplicate
JSONObject serviceObject2 = new JSONObject();
System.out.println(serviceObj.get("service"));
System.out.println("-----------");
list.add(obj);
serviceObject2.put("shape", "image");
serviceObject2.put("label", service);
File f = new File("images/" + service + ".png");
serviceObject2.put("image", "images/Circle-icon.png");
serviceObject2.put("widthMin", 15);
serviceObject2.put("widthMax", 15);
serviceObject2.put("x", 100);
serviceObject2.put("y", 100);
serviceObject2.put("title", "Server/Host: " + host + "\n" + "Port :" + port);
serviceObject2.put("value", 1);
serviceObject2.put("port", port);
serviceObject2.put("protocol", protocol);
serviceNodeList.add(serviceObject2);
keys.add(service); //Add key to set
}
}
}
List serviceNodeList=new ArrayList();
JSONObject jsonObj=新的JSONObject(“读取json数据”);
JSONArray数组=jsonObj.getJSONObject(“hits”).getJSONArray(“hits”);
Set keys=new HashSet();//创建哈希集
对于(int i=0;i