使用 Java 中的条件从数组中的 JSON 获取值
Get Values from JSON within Array using condition in Java
我正在使用 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
但是,我需要得到 id's
使用此条件使用 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"));
}
有人可以帮我解决这个问题吗?
只需像这样更改您的 for 循环 :-
for (int j = 0; j < len; j++) {
JSONObject json = jArray.getJSONObject(0);
if(json.getInt("isDeleted") == 0 ){
ids.add((Integer) json.get("id"));
}
}
我正在使用 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
但是,我需要得到 id's
使用此条件使用 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"));
}
有人可以帮我解决这个问题吗?
只需像这样更改您的 for 循环 :-
for (int j = 0; j < len; j++) {
JSONObject json = jArray.getJSONObject(0);
if(json.getInt("isDeleted") == 0 ){
ids.add((Integer) json.get("id"));
}
}