从json文件中删除子属性 [英] Delete a child attribute from json file

查看:204
本文介绍了从json文件中删除子属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Java中具有以下HTTP JSON响应,它代表一个用户对象.

I have the following HTTP JSON-response in Java, which represents a user object.

{
    "account": "Kpatrick",
    "firstname": "Patrick",
    [
    ],
    "instances":
    [
        {
            "id": "packerer-pool",
            "key": "packerer-pool123",
            "userAccount": "kpatrick",
            "firstname": "Patrick",
            "lastname": "Schmidt",

        }
    ],
    "projects":
    [
        {
            "id": "packerer-projectPool",
            "projectKey": "projectPool-Pool",
            "cqprojectName": "xxxxx",
        },
       {
            "id": "packerer-secondproject",
            "projectKey": "projectPool-Pool2",
            "cqprojectName": "xxxx",

        },
        {
            "id": "packerer-thirdproject",
            "projectKey": "projectPool-Pool3",
            "cqprojectName": "xxxx",
        }
    ],

    "clients":
    [
    ],
    "dbid": 76864576,
    "version": 1,
    "id": "dbpack21"
}

现在,我想在projectkey的帮助下搜索特定项目(例如"projectPool-Pool2").之后,我想完全删除该元素.因为我的目标是在没有该项目的情况下发送HTTP呼叫.

Now, I want to search a specific project with the help of the projectkey (for example "projectPool-Pool2"). After that, I want to delete the element completely. Because my target is to send a HTTP post-call without this project.

对于我的HTTP调用,结果应类似于以下内容:

The result should be similar to below for my HTTP post-call:

    {
    "account": "Kpatrick",
    "firstname": "Patrick",
    [
    ],
    "instances":
    [
        {
            "id": "packerer-pool",
            "key": "packerer-pool123",
            "userAccount": "kpatrick",
            "firstname": "Patrick",
            "lastname": "Schmidt",

        }
    ],
    "projects":
    [
        {
            "id": "packerer-projectPool",
            "projectKey": "projectPool-Pool",
            "cqprojectName": "xxxxx",
        },
        {
            "id": "packerer-thirdproject",
            "projectKey": "projectPool-Pool3",
            "cqprojectName": "xxxx",
        }
    ],

    "clients":
    [
    ],
    "dbid": 76864576,
    "version": 1,
    "id": "dbpack21"
}

首先,我已经解析了对字符串的响应.

First i have parsed the response to a string.

    private static String getContent(HttpResponse response) {
    HttpEntity entity = response.getEntity();
    if (entity == null) return null;
    BufferedReader reader;
    try {
        reader = new BufferedReader(new InputStreamReader(entity.getContent()));
        String line = reader.readLine();
        reader.close();
        return line;
    } catch (IllegalStateException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

现在我正在尝试搜索特定项目,但是我不知道如何继续.

And now i am trying to search the specific project, but i don't know how to continue.

String StringResponse = getContent(JsonResponse);
JSONObject jsonObject = new JSONObject(StringResponse);
JSONArray ProjectsArray= jsonObject.getJSONArray("projects");

这种方法正确吗?

最诚挚的问候!

推荐答案

一旦有了数组,请尝试类似...

Once you have your array, try something like...

// Array to store the indexes of the JSONArray to remove
ArrayList<Integer> indexesToRemove = new ArrayList<Integer>();
// Iterate through projects array, check the object at each position
// if it contains the string you want, add its index to the removal list
for (int i = 0; i < projectsArray.length; i++) {
    JSONObject current = projectsArray.get(i);
    if (current.get("projectKey") == "**DESIRED PROJECT KEY**") {
        indexesToRemove.add(i);
    } 
}

现在,您可以遍历索引以进行删除,并使用JSONArrays remove方法从数组中删除相应的对象(不知道它叫什么,上面的代码来自内存).请确保向后删除您的项目,否则,您将删除较早的项目,这将更改索引,如果您随后再删除另一个索引,则会删除不正确的项目.

Now you can iterate through your indexes to remove, and remove the corresponding object from the array with the JSONArrays remove method (not sure what it is called, the code above is from memory). Make sure to remove your items BACKWARDS, otherwise you will delete earlier items, which will change the indexes, resulting in your removing an incorrect item if you then remove another index.

// Going through the list backwards so we can remove the highest item each 
//time without affecting the lower items
for (int i = indexesToRemove.size()-1; i>=0; i--) {
    projectsArray.remove(indexesToRemove.get(i));
}

这篇关于从json文件中删除子属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆