首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何使用org.json.simple从json数组中移除json对象

如何使用org.json.simple从json数组中移除json对象
EN

Stack Overflow用户
提问于 2020-07-03 16:51:46
回答 3查看 390关注 0票数 1

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

我使用的是库org.json.simple。下面是我的json文件:

代码语言:javascript
运行
复制
  [ {       "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": ""
        }
     ]

这就是我被卡住的地方。:

代码语言:javascript
运行
复制
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");          
        }
    }
    ...

有什么建议吗?我该如何更改我的代码?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2020-07-03 20:09:21

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

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

代码语言:javascript
运行
复制
@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;
    }
}

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

代码语言:javascript
运行
复制
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;
    }
}
票数 1
EN

Stack Overflow用户

发布于 2020-07-03 17:27:08

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

票数 1
EN

Stack Overflow用户

发布于 2020-07-03 20:20:03

代码语言:javascript
运行
复制
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();
        }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62711849

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档