java - if else 如何使用 java 从 JSON 文件中删除重复元素?

标签 java json duplicates if-statement

在我的代码中找到的这个开始来 self 的 JSON 文件。现在,在我的 JSON 文件中,我有很多同名的“start”。例如,在此 JSON 文件中,我在开头有重复项(内容均为“audi”):

{"rel": "AtLocation", "weight": 0.5, "dataset": "/d/dbpedia/en", "sources": ["/s/dbpedia/3.7"], "id": "0209765bf7185615ef242513f61ca0f61efe2a04", "features": ["audi AtLocation -", "audi - curitiba", "- AtLocation curitiba"], "end": "curitiba", "license": "/l/CC/By-SA", "uri": "/a/[AtLocation/,audi/,curitiba/]", "start": "audi", "context": "/ctx/all", "surfaceText": null}

{"rel": "AtLocation", "weight": 0.5, "dataset": "/d/dbpedia/en", "sources": ["/s/dbpedia/3.7"], "id": "5158d1cfce728efb3e42840d166ec06153a3d77d", "features": ["audi AtLocation -", "audi - ingolstadt", "- AtLocation ingolstadt"], "end": "unite_state", "license": "/l/CC/By-SA", "uri": "/a/[AtLocation/,audi/,ingolstadt/]", "start": "audi", "context": "/ctx/all", "surfaceText": null}

这是我的 Java 代码:

String start = (String) jsonObject.get("start");  
StartVertex startV = new StartVertex(start);  
helloGraphDB.addVertex(startV);    

我不希望它添加同一个“audi”的Vertex 2次,我该如何编写逻辑if else out?

谢谢。

最佳答案

`您可以使用arraylist检查值是否已经插入到addVertex

喜欢:

ArrayList addedVertex_Val = new ArrayList();

    String start = (String) jsonObject.get("start");
    boolean shouldInsert = true;

    for (int al_length = 0; al_length < addedVertex_val.size(); al_length++) {
        String val = addedVertex_val.get(al_length).toString();
        if (val.equals(start)) {
            shouldInsert = false;
            break;
        }
    }

    if (shouldInsert) {
        StartVertex startV = new StartVertex(start);
        helloGraphDB.addVertex(startV);
        addedVertex_Val.add(startV);
    }

`

关于java - if else 如何使用 java 从 JSON 文件中删除重复元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12598713/

相关文章:

java - 在排序的链表中查找重复项

java - 如何从java中的两个数组中删除重复的字符串?

java - Java 中的随机访问文件和额外 ASCII 字符

java - 在没有 Grails 的普通 Spring MVC 中使用 GSP View

javascript - 从页面 URL 获取 Open Graph 对象 ID

c++ - 使用 Boost property_tree 读取 JSON

json - mongoose find() 输出隔离

java - 通过 SSH 连接到 docker 容器并执行 Java 程序 (PSVM)

java - 生成在空格之间添加 + 号作为每一行的一个字符串的模式

git - 不小心创建了具有现有名称的新分支