【问题标题】:how to remove json object from json Array using org.json.simple如何使用 org.json.simple 从 json 数组中删除 json 对象
【发布时间】:2020-07-03 08:51:46
【问题描述】:

我有一个 JSON 文件,它是一个带有一些 JSON 对象的 JSON ARRAY,如果经度或纬度的值为空字符串“”,我想删除整个 json 对象。

我正在使用库 org.json.simple。这是我的 json 文件:

  [ {       "Longitude": 9.96233,
            "Latitude": 49.80404 },
    
    {
            "Longitude": 6.11499,
            "Latitude": 50.76891
        
    },
     {      "Longitude": 6.80592,
            "Latitude": 51.53548
    },
     {
            "Longitude": 9.50523,
            "Latitude": 51.31991   },
     {
            "Longitude": ""
            "Latitude": ""
       
    },
     {
            "Longitude": 9.93368,
            "Latitude": ""
       
    },
    {
            "Longitude": 11.56122,
            "Latitude": 48.14496
      
    },

     {
            "Longitude": 13.34253,
            "Latitude": 52.5319
        
    },
     {
            "Longitude": 6.11327,
            "Latitude": 50.77715
      
    },
     {
            "Longitude": ""
            "Latitude": ""
        }
     ]

这就是我卡住的地方。 :

JSONParser jsonParser = new  JSONParser();
try (FileReader reader = new FileReader ("output.json")) {
    Object obj = jsonParser.parse(reader);
    JSONArray list = (JSONArray) obj;
    list.forEach(node -> {
        String vari = (String)((JSONObject)node).get("longitude").toString();            
        if (vari==null) {
            ((JSONObject) node).remove();
            System.out.println("deleted");          
        }
    }
    ...

任何建议如何更改我的代码?

【问题讨论】:

  • 根据您的 JSON 文件,该结构不是对象数组。所有对象都包装在一个对象中,并且该对象被添加到数组中。因此,实际上您的列表中只有一个元素。请使用 json editor 验证您的 JSON。

标签: java json org.json


【解决方案1】:

您需要从列表中删除节点,但由于并发修改问题(修改您正在循环的列表),您需要另一个列表。

我认为将所有符合条件的节点收集到新列表中是最容易的,如下所示:

@SuppressWarnings("unchecked")
@Test
public void test() throws Exception {
    JSONParser jsonParser = new JSONParser();
    JSONArray listNonNull = new JSONArray();
    try (FileReader reader = new FileReader("output.json")) {
        JSONArray list = (JSONArray) jsonParser.parse(reader);
        ((Collection<JSONObject>)list).forEach(node -> {
            // here any check that qualifies the node like also checking "Latitude"
            // Also no need to cast to a String
            Object vari = node.get("Longitude");
            if (vari != null && !vari.equals("")) {
                listNonNull.add(node);
            }
        });
    } catch (Exception e) {
        throw e;
    }
}

如果您只想使用原始列表,您可以将要删除的项目收集到另一个列表中,并使用它从原始列表中删除节点:

public void test2() throws Exception {
    JSONParser jsonParser = new JSONParser();
    JSONArray toBeRemoved = new JSONArray();
    try (FileReader reader = new FileReader("output.json")) {
        JSONArray list = (JSONArray) jsonParser.parse(reader);
        ((Collection<JSONObject>) list).forEach(node -> {
            Object vari = node.get("Longitude");
            // here any check that qualifies the node like also checking "Latitude"
            if (vari != null && !vari.equals("")) {
                return; // not to be removed
            }
            toBeRemoved.add(node);
        });
        list.removeAll(toBeRemoved);
    } catch (Exception e) {
        throw e;
    }
}

【讨论】:

  • @pirho 谢谢你的回复,我应该用这个替换我的列表吗?因为我还在前一个列表上进行了一些修改,所以我必须对其节点使用相同的列表。我应该将此方法添加到我的脚本中并在 main 中调用它吗?
  • 因为当我尝试使用它时,我的代码中出现了很多错误。
  • 我可以修改我的答案以保留原始列表。但不知何故,我认为你应该先做这个 mod,然后只做剩下的,以避免循环元素,你以后实际删除。因此,如果您可以将其作为第一个 mod 执行此操作,请用新列表替换列表。 @Paupiette
【解决方案2】:

在代码中使用.remove 而不是.clear。

【讨论】:

  • 我尝试使用 remove() 但它只删除了密钥,我无法删除整个对象。
【解决方案3】:
JSONParser jsonParser = new JSONParser();
        try (FileReader reader = new FileReader("output.json")) {
            Object obj = jsonParser.parse(reader);
            JSONArray list = (JSONArray) obj;

            ArrayList<JSONObject> objList = (ArrayList<JSONObject>) list.stream().filter(node -> {
                String longitude = ((JSONObject) node).get("Longitude").toString();
                String latitude = ((JSONObject) node).get("Latitude").toString();

                if (longitude.isEmpty() || latitude.isEmpty()) {
                    return false;
                }
                return true;
            }).collect(Collectors.toList());
        } catch (IOException | ParseException e) {
            e.printStackTrace();
        }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-23
    • 1970-01-01
    • 2018-11-13
    • 1970-01-01
    • 2023-02-15
    • 1970-01-01
    相关资源
    最近更新 更多