Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Get Values from JSON within Array using condition in Java

I am using Unirest, to get all id’s from an end point,
My Array is like this:

[
    {
        "date": "2022-04-05",
        "timeIn": "2022-04-05 07:00:00",
        "timeOut": "2022-04-05 14:00:00",
        "createdAt": "2022-04-05 08:32:59",
        "updatedAt": "2022-04-06 13:42:23",
        "deletedAt": "2022-04-06 13:42:23",
        "id": 3984193,
        "userId": 42602,
        "storeId": 1845,
        "positionId": 8161,
        "clockInIP": null,
        "clockOutIP": null,
        "isDeleted": 1,
        "timeInEdited": 1
    },
    {
        "date": "2022-04-04",
        "timeIn": "2022-04-04 07:00:00",
        "timeOut": "2022-04-04 14:00:00",
        "createdAt": "2022-04-06 13:36:03",
        "updatedAt": "2022-04-06 13:36:03",
        "deletedAt": null,
        "id": 3984196,
        "userId": 42602,
        "storeId": 1845,
        "positionId": 8161,
        "clockInIP": null,
        "clockOutIP": null,
        "isDeleted": 0,
        "timeInEdited": 1
    }
]

Now I want to get all id's , where isDeleted is 0, I am using a logic, which will get id's but , I need to get id's using this condition using Java.

            JSONArray jArray = bodyResponse.getBody().getArray();
            int len = jArray.length();
            for (int j = 0; j < len; j++) {
                JSONObject json = jArray.getJSONObject(0);
                ids.add((Integer) json.get("id"));
}

Can someone help me out on this?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Just change your for Loop like this :-

for (int j = 0; j < len; j++) {
    JSONObject json = jArray.getJSONObject(0);
    if(json.getInt("isDeleted") == 0 ){
        ids.add((Integer) json.get("id"));
    }
 }
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading