gpt4 book ai didi

java - 添加到 Json 数组时数据会被覆盖

转载 作者:行者123 更新时间:2023-12-02 06:12:05 26 4
gpt4 key购买 nike

我有来自 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;
}```

最佳答案

您在“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;
}

关于java - 添加到 Json 数组时数据会被覆盖,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55900609/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com