0

I have an ArrayList containing a list of JSONArrays

staffArray = new ArrayList<JSONArray>();

the JSONArray is in a form of this:

[
    {
      "id": "k40dn-dff02-mm1",
      "name": "staff1",
      "tel": "0123456789",
    },
    {
      "id": "ch2mq-pmw01-ps6",
      "name": "staff2",
      "tel": "9876543210",
    }
    ...
]

And the ArrayList will be containing different sizes of JSONArray. Now I want to check in the ArrayList for each JSONArray, if they contain the same value for "id". So say that if the ArrayList has three different sizes of JSONArray, how can I tell the they each contain a JSONObject with the same value for "id" in it.

So far I have tried this to extract the string:

for(int i = 0; i < staffArray.size(); i++){
    JSONArray jsonArray = new JSONArray();
    jsonArray = staffArray.get(i);
    for(int j = 0; j < jsonArray.length(); j ++){
        JSONObject json = null;
        try {
            json = jsonArray.getJSONObject(j);
            String id = json.getString("id");
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}
0

2 Answers 2

1

If you would like to check for duplicate IDs in your ArrayList, you could do something like this:

ArrayList<JSONArray> staffArray = new ArrayList<>();

Set<String> ids = new HashSet<>();
for (JSONArray array : staffArray) {
    for (int i = 0; i < array.length(); i++) {
        JSONObject obj = array.getJSONObject(i);
        if (!ids.add(obj.getString("id"))) {
            // duplicate IDs found, do something
        }
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your reply, what I want is to find duplicate in different JSONArrays, say that ArrayList contains three JSONArrays and check if each JSONArray has a key "id" with the same value
@JerryKo I see. You just have to move the Set of IDs outside the outer loop then. See my edited answer.
0

How about using group by to group the json arrays with same id something like this

public static void groupById(List<JSONArray> staffArray) {
    Map<String, List<JSONArray>> jsonArraysById = staffArray.stream().collect(Collectors.groupingBy(jsonArray -> getIdFromJsonArray(jsonArray)));
    jsonArraysById.forEach((id, arrays) -> {
        System.out.println("Arrays with id " + id + " are " + arrays);
    });
}

public static String getIdFromJsonArray(JSONArray jsonArray) {
    String result = null;
    for (int j = 0; j < jsonArray.length(); j++) {
        JSONObject json = null;
        try {
            json = jsonArray.getJSONObject(j);
            result = json.getString("id");
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    return result;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.