Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check JSONArray for specific value

Tags:

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?

like image 321
tobias Avatar asked Dec 28 '12 15:12

tobias


People also ask

How do I read JSONArray?

String value = (String) jsonObject. get("key_name"); Just like other element retrieve the json array using the get() method into the JSONArray object.

How do I check if a string is Jsonarject or JSONArray?

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..


1 Answers

use a simple String function/method like

private boolean userexists(JSONArray jsonArray, String usernameToFind){     return jsonArray.toString().contains("\"username\":\""+usernameToFind+"\""); } 
like image 122
petey Avatar answered Sep 22 '22 15:09

petey