1

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

{
    "name": "Jane John Doe",
    "email": "[email protected]"
},
{
    "name": "Jane Doe",
    "email": "[email protected]"
},
{
    "name": "John Doe",
    "email": "[email protected]"
}

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);
}
3
  • Seems like your JSON is not a JSONObject , but a JSONArray. Commented Feb 4, 2018 at 20:59
  • JSONArray jsonArray = new JSONArray(json); should solve your problem i think. Then loop and place them in the array. Commented Feb 4, 2018 at 21:02
  • I got a type mismatch error, json object couldn't be converted to json array. Commented Feb 4, 2018 at 21:41

2 Answers 2

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": "[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();
    }
Sign up to request clarification or add additional context in comments.

1 Comment

Yea, that way I can hold each item separately and operate on them.
1

Your JSON is not a JSONObject, but a JSONArray,

your response should like this Valid JSON

[{
    "name": "Jane John Doe",
    "email": "[email protected]"
}, {
    "name": "Jane Doe",
    "email": "[email protected]"
}, {
    "name": "John Doe",
    "email": "[email protected]"
}]

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.