使用 Java 中的条件从数组中的 JSON 获取值

发布于 2025-01-19 13:56:44 字数 1416 浏览 1 评论 0原文

我正在使用 Unirest,从端点获取所有 id, 我的数组是这样的:

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

现在我想获取所有 id's ,其中 isDeleted 为 0,我正在使用一个逻辑,它将获取 id's但是,我需要使用 Java 使用此条件来获取 id。

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

有人可以帮我解决这个问题吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

月下凄凉 2025-01-26 13:56:44

像这样更改您的循环: -

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

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"));
    }
 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文