mardi 12 juillet 2016

JSONObject to arraylist? Why this JSONException?

I have this json

{"summary":{"total_count":2},"data":[{"id":"129393910815742","name":"Mike Pollito"},{"id":"117161088707629","name":"James Carballo"}],"paging":{"cursors":{"after":"mUhn","before":"QVFn"}}}

and I need to save the ids in an ArrayList or any simple array.

Note:The number of ids may vary.

The problem is than i can easily get "total_count" with

int amigos = listatodoslosamigos.getJSONObject("summary").getInt("total_count");

but when i try to get any "id" when i call

JSONObject data = listatodoslosamigos.getJSONObject("data");

i always get a JSONException

This is my buggy code

public void onCompleted(GraphResponse response) {
        /* handle the result */
                     JSONObject listatodoslosamigos = response.getJSONObject();
                    try {
                        int amigos = listatodoslosamigos.getJSONObject("summary").getInt("total_count");
                        JSONObject data = listatodoslosamigos.getJSONObject("data");

                    } catch (JSONException e) {}
                }
            }
    ).executeAsync();

Aucun commentaire:

Enregistrer un commentaire