[
{
"UserID": "vishi" // key is UserId. value is vishi
},
{
"UserID": "vish"
}
]
The key UserID
is the same. Just loop through the array and get the value
ArrayList<String> list = new ArrayList<String>();
JSONArray jr = new JSONArray("your json");
for(int i=0i<jr.length();i++)
{
JSONObject jb = jr.getJSONObject(i);
String value= jb.getString("UserID");
list.add(value);
}
Note:
Blackbelt's answer will also work and it also has null check cause optJSONObject()
could return null also. This is a better way
Drawing from blackbelt's answer
JSONObject obj = array.optJSONObject(i);
if (obj != null) {
String userId = obj.optString("UserID");
}
From the docs
public JSONObject optJSONObject (String name)
Added in API level 1
Returns the value mapped by name if it exists and is a JSONObject. Returns null otherwise.