【问题标题】:How to avoid adding duplicate entries to Jsonobject如何避免向 Jsonobject 添加重复条目
【发布时间】:2018-10-04 10:10:49
【问题描述】:

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

【问题讨论】:

  • 不将 Json 对象转换为任何 Java 对象?如果您转换为 java 对象并在列表中添加服务项目。您可以使用以下链接删除重复的stackoverflow.com/questions/29670116/…
  • 您用于 JSONObject 的库是什么?这个类是否实现了equals和hashCode?如果是这样,那么只需使用Set 而不是List 作为serviceNodeList
  • @Seigil Bishyr 用于 JSONObject 的库是 org.json,我知道如果我使用 Set 它不会允许重复,但每次我必须将数据放入 JSONObject 时,我都必须了解 rhich服务正在重复。

标签: java arrays json list


【解决方案1】:

通过在您的类中覆盖 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 ()

【讨论】:

  • 感谢您的宝贵时间,我会试试这个并告诉您。
【解决方案2】:

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

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

【讨论】:

    猜你喜欢
    • 2023-03-22
    • 2020-08-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-24
    • 1970-01-01
    • 2022-01-08
    相关资源
    最近更新 更多