首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从json文件中删除子属性

从json文件中删除子属性
EN

Stack Overflow用户
提问于 2016-09-03 00:13:28
回答 1查看 713关注 0票数 0

我在Java中有以下HTTP JSON-response,它表示一个用户对象。

代码语言:javascript
复制
{
    "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 post调用。

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

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

首先,我将响应解析为一个字符串。

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

现在我正在尝试搜索具体的项目,但我不知道如何继续。

代码语言:javascript
复制
String StringResponse = getContent(JsonResponse);
JSONObject jsonObject = new JSONObject(StringResponse);
JSONArray ProjectsArray= jsonObject.getJSONArray("projects");

这种方法正确吗?

诚挚的问候!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-09-03 00:23:34

一旦你有了你的数组,试着这样做...

代码语言:javascript
复制
// 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方法从数组中删除相应的对象(不确定它是什么名称,上面的代码是从内存中删除的)。确保向后删除您的项目,否则将删除以前的项目,这将更改索引,如果您随后删除另一个索引,则会导致删除不正确的项目。

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

https://stackoverflow.com/questions/39296779

复制
相关文章

相似问题

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