As per your code you just get the key and value of the first object.
JSONObject jsonObject = new JSONObject(json);
from above code, you just get the first object from JSON.
Iterator<?> iterator = jsonObject.keys();
while (iterator.hasNext()) {
Object key = iterator.next();
Object value = jsonObject.get(key.toString());
response.add(value);
}
From above code, you will get the key and values of first JSON object.
Now, if you want to get all object value then you have to change your code as per below :
Change JSON like below:
[{
"name": "Jane John Doe",
"email": "[email protected]"
},
{
"name": "Jane Doe",
"email": "[email protected]"
},
{
"name": "John Doe",
"email": "[email protected]"
}]
Change java code as per below:
List<HashMap<String,String>> response = new ArrayList<>();
JSONArray jsonarray = null;
try {
jsonarray = new JSONArray(json);
for(int i=0;i<jsonarray.length();i++) {
JSONObject jsonObject = jsonarray.getJSONObject(i);
Iterator<?> iterator = jsonObject.keys();
HashMap<String,String> map = new HashMap<>();
while (iterator.hasNext()) {
Object key = iterator.next();
Object value = jsonObject.get(key.toString());
map.put(key.toString(),value.toString());
}
response.add(map);
}
} catch (JSONException e) {
e.printStackTrace();
}
JSONis not aJSONObject, but a JSONArray.