如何避免向 Jsonobject 添加重复条目



我在Elasticsearch数据库中有以下数据集

"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);


}

}

在这里,我不想向 jsonObject 添加重复的服务。

通过在类中重写 Object 类的 .equals(( 和 toString(( 方法,您可以实现这一点。

public class Test {
String service;
@Override
public String toString() {
System.out.println("to string");
return service;
}
@Override
public boolean equals(Object obj) {
String name1 = (String) ((Test) obj).service;
return service.equals(name1);
}
public static void main(String[] args) {
JSONArray array = jsonObj.getJSONObject("hits").getJSONArray("hits");
List<Test> list = new ArrayList<Test>();
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 serviceObject = new JSONObject();
JSONObject serviceObj = serviceList.getJSONObject(j);
Test test = new Test();
test.service = serviceObj.getString("service");
list.add(test);                   
String service = (String) serviceObj.get("service");
if (!list.contains(test)) {
System.out.println(serviceObj.get("service"));
System.out.println("-----------");
list.add(test);
serviceObject.put("service",service);
serviceNodeList.add(serviceObject2);
}
}
}
System.out.println(serviceNodeList);
} catch (Exception e) {
e.printStackTrace();
}
return serviceNodeList;
}

要检查列表是否包含对象,我们可以使用 list 类的 .contains((,它在内部调用.equals((,它在内部调用 string((

如果我理解正确,您不想添加如下所示的服务,因为这些服务对于每个主机都是重复的。

{
"port": "80",
"protocol": "tcp",
"status": "open",
"service": "http"
}

你可以使用 java.util.HashSet,并继续在 set 中添加键(比如"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
}
}
}

最新更新