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?
String value = (String) jsonObject. get("key_name"); Just like other element retrieve the json array using the get() method into the JSONArray object.
JSONObject json = RestManager. getJSONfromURL(myuri); // retrieve the entire json stream JSONArray interventionJsonArray = json. getJSONArray("intervention"); In the first case, the above doesn't work because there is only one element in the stream..
use a simple String function/method like
private boolean userexists(JSONArray jsonArray, String usernameToFind){ return jsonArray.toString().contains("\"username\":\""+usernameToFind+"\""); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With