从json文件

时间:2016-09-02 16:13:28

标签: java json qjsonobject

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

{
    "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后呼叫。

对于我的HTTP后呼叫,结果应类似于以下内容:

    {
    "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"
}

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

    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;
}

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

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

这种做法是否正确?

最诚挚的问候!

1 个答案:

答案 0 :(得分:0)

一旦你有阵列,尝试类似......

// 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方法从数组中删除相应的对象(不确定它的名称,上面的代码来自内存)。确保删除项目BACKWARDS,否则您将删除先前的项目,这将更改索引,导致您删除不正确的项目,如果您删除另一个索引。

// 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));
}