android – Check JSONArray for specific value-ThrowExceptions

Exception or error:

In my Android Activity I am getting a JSONArray via HTTP containg usernames.
The Array looks like this:

[{"username":"Julia"},{"username":"Anja"},{"username":"Hans"},{"username":"Sophia"},{"username":"Sarah"}]

I want to check in the Android Activity if a given username already exists.

What would be the most efficient way to do it? Or do I have to iterate over the whole array?

How to solve:

use a simple String function/method like

private boolean userexists(JSONArray jsonArray, String usernameToFind){
    return jsonArray.toString().contains("\"username\":\""+usernameToFind+"\"");
}

Leave a Reply

Your email address will not be published. Required fields are marked *