1

How can I loop through JSON with this type of structure:

{
    "name": "Jane John Doe",
    "email": "janejohndoe@email.com"
},
{
    "name": "Jane Doe",
    "email": "janedoe@email.com"
},
{
    "name": "John Doe",
    "email": "johndoe@email.com"
}

I tried the below code but I only got the last item.

List<Object> response = new ArrayList<>();

JSONObject jsonObject = new JSONObject(json);
Iterator<?> iterator = jsonObject.keys();
while (iterator.hasNext()) {
    Object key = iterator.next();
    Object value = jsonObject.get(key.toString());

    response.add(value);
}
Joshua
  • 589
  • 1
  • 5
  • 19

2 Answers2

3

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": "janejohndoe@email.com"
},
{
  "name": "Jane Doe",
  "email": "janedoe@email.com"
},
{
  "name": "John Doe",
  "email": "johndoe@email.com"
}]

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();
    }
Pratik Satani
  • 1,115
  • 1
  • 9
  • 25
0

Your JSON is not a JSONObject, but a JSONArray,

your response should like this Valid JSON

[{
    "name": "Jane John Doe",
    "email": "janejohndoe@email.com"
}, {
    "name": "Jane Doe",
    "email": "janedoe@email.com"
}, {
    "name": "John Doe",
    "email": "johndoe@email.com"
}]

Parsing Snippet of code

try
{
    JSONArray jsonArray = new JSONArray("Your Json Response");

    for (int i = 0; i < jsonArray.length(); i++) {

        JSONObject jsonObject = jsonArray.getJSONObject(i);

        String userName = jsonObject.getString("name");
        String userEmail = jsonObject.getString("email");

        Log.i("TAG Name " + i, " : " + userName);
        Log.i("TAG Mail " + i, " : " + userEmail);

    }
} 
catch (JSONException e) 
{
    // TODO Auto-generated catch block
    e.printStackTrace();
}

OUTPUT

enter image description here

Ratilal Chopda
  • 4,162
  • 4
  • 18
  • 31