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

时间:2018-10-04 10:10:50

标签: java arrays json list

我在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添加重复的服务。

2 个答案:

答案 0 :(得分:2)

通过在您的类中重写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;
}

要检查列表是否包含对象,我们可以使用列表类的 .contains(),该类内部调用 .equals(),内部调用 toString ()

答案 1 :(得分:0)

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

       {
          "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
        }
    }
}