添加到Json Array时数据被覆盖

时间:2019-04-29 09:37:45

标签: java json

我有来自Splunk的数据,数据是带有端口的主机,有些主机具有多个端口。所以我做了下面的逻辑。对于我添加到jsonarray的每个具有单个端口的主机。问题是端口在添加到jsonarray的那一刻总是被覆盖,我想知道为什么?

 public JSONObject parse_OS_Vuln_data_to_Json() {
        JSONObject responseDetailsJson = new JSONObject();
        JSONArray jsonArray = new JSONArray();
        if (list_of_OS_VulnerabilityOS.size()>0)
        {
            for (int i = 0; i < list_of_OS_VulnerabilityOS.size(); i++) {
                JSONObject formDetailsJson = new JSONObject();
                if (list_of_OS_VulnerabilityOS.get(i).getList_of_ports().size() > 1)
                {
                    for (int v = 0; v < list_of_OS_VulnerabilityOS.get(i).getList_of_ports().size(); v++)
                    {
                        formDetailsJson.put("host", list_of_OS_VulnerabilityOS.get(i).getDest());
                        formDetailsJson.put("port", list_of_OS_VulnerabilityOS.get(i).getList_of_ports().get(v)); // i got each time different port 
                        formDetailsJson.put("open", list_of_OS_VulnerabilityOS.get(i).isOpen());
                        formDetailsJson.put("info", list_of_OS_VulnerabilityOS.get(i).getInfo()); 
                        jsonArray.add(formDetailsJson); // here it overwrites to the first port again  
                        total_hostNumber_OS++;
                    }
                } else {
                    formDetailsJson.put("host", list_of_OS_VulnerabilityOS.get(i).getDest());
                    formDetailsJson.put("port", list_of_OS_VulnerabilityOS.get(i).getPorts___port());
                    formDetailsJson.put("open", list_of_OS_VulnerabilityOS.get(i).isOpen());
                    formDetailsJson.put("info", list_of_OS_VulnerabilityOS.get(i).getInfo());
                    jsonArray.add(formDetailsJson);
                    total_hostNumber_OS++;
                }
        }
            responseDetailsJson.put("data", jsonArray);
            System.out.print("\n list size: " + list_of_OS_VulnerabilityOS.size());
            System.out.print("\n total of OS-host to be send : " + total_hostNumber_OS);

        }
        return responseDetailsJson;
    }```

2 个答案:

答案 0 :(得分:0)

您在“ v”循环中引用了相同的formDetailsJson实例。这意味着每次调用“ put”时,它都在同一个对象上(这意味着不仅您的端口应被覆盖,而且所有其他字段也应被覆盖)。您jsonArray中的所有条目都将是对同一实例的引用。

最好在循环内为list_of_OS_VulnerabilityOS分配一个变量,而不要如此重复。

代码看起来像这样:

    public JSONObject parse_OS_Vuln_data_to_Json() {
        JSONObject responseDetailsJson = new JSONObject();
        JSONArray jsonArray = new JSONArray();
        if (list_of_OS_VulnerabilityOS.size()>0) {
            for (int i = 0; i < list_of_OS_VulnerabilityOS.size(); i++) {
                WhateverTheTypeIs currentOSVulnerability = list_of_OS_VulnerabilityOS.get(i);
                JSONObject formDetailsJson;

                if (currentOSVulnerability .getList_of_ports().size() > 1) {
                    for (int v = 0; v < list_of_OS_VulnerabilityOS.get(i).getList_of_ports().size(); v++) {
                        formDetailsJson = new JSONObject() // Here's the fix: assign inside of the loop, so it's a new instance
                        formDetailsJson.put("host", currentOSVulnerability.getDest());
                        formDetailsJson.put("port", currentOSVulnerability.getList_of_ports().get(v));
                        formDetailsJson.put("open", currentOSVulnerability.isOpen());
                        formDetailsJson.put("info", currentOSVulnerability.getInfo());
                        jsonArray.add(formDetailsJson);
                        total_hostNumber_OS++;
                    }
                } else {
                    formDetailsJson = new JSONObject();
                    formDetailsJson.put("host", currentOSVulnerability.getDest());
                    formDetailsJson.put("port", currentOSVulnerability.getPorts___port());
                    formDetailsJson.put("open", currentOSVulnerability.isOpen());
                    formDetailsJson.put("info", currentOSVulnerability.getInfo());
                    jsonArray.add(formDetailsJson);
                    total_hostNumber_OS++;
                }
            }
            responseDetailsJson.put("data", jsonArray);
            System.out.print("\n list size: " + list_of_OS_VulnerabilityOS.size());
            System.out.print("\n total of OS-host to be send : " + total_hostNumber_OS);
        }
        return responseDetailsJson;
    }

答案 1 :(得分:0)

 public JSONObject parse_OS_Vuln_data_to_Json() {
    JSONObject responseDetailsJson = new JSONObject();
    JSONArray jsonArray = new JSONArray();
    if (list_of_OS_VulnerabilityOS.size()>0)
    {
        for (int i = 0; i < list_of_OS_VulnerabilityOS.size(); i++) {
            JSONObject formDetailsJson = null; // change #1
            if (list_of_OS_VulnerabilityOS.get(i).getList_of_ports().size() > 1)
            {
                for (int v = 0; v < list_of_OS_VulnerabilityOS.get(i).getList_of_ports().size(); v++)
                {
                    formDetailsJson = new JSONObject();  // change #2
                    formDetailsJson.put("host", list_of_OS_VulnerabilityOS.get(i).getDest());
                    formDetailsJson.put("port", list_of_OS_VulnerabilityOS.get(i).getList_of_ports().get(v)); // i got each time different port 
                    formDetailsJson.put("open", list_of_OS_VulnerabilityOS.get(i).isOpen());
                    formDetailsJson.put("info", list_of_OS_VulnerabilityOS.get(i).getInfo()); 
                    jsonArray.add(formDetailsJson); // here it overwrites to the first port again  
                    total_hostNumber_OS++;
                }
            } else {
                formDetailsJson = new JSONObject();  // change #3
                formDetailsJson.put("host", list_of_OS_VulnerabilityOS.get(i).getDest());
                formDetailsJson.put("port", list_of_OS_VulnerabilityOS.get(i).getPorts___port());
                formDetailsJson.put("open", list_of_OS_VulnerabilityOS.get(i).isOpen());
                formDetailsJson.put("info", list_of_OS_VulnerabilityOS.get(i).getInfo());
                jsonArray.add(formDetailsJson);
                total_hostNumber_OS++;
            }
    }
        responseDetailsJson.put("data", jsonArray);
        System.out.print("\n list size: " + list_of_OS_VulnerabilityOS.size());
        System.out.print("\n total of OS-host to be send : " + total_hostNumber_OS);

    }
    return responseDetailsJson;
}

在#2 for循环中,您正在使用相同的formDetailsJson对象。因此,每次循环运行时,您的formDetailsJson对象都会被覆盖。