2016-06-12 10 views
0

Ich habe dieses jsonJSONObject zu Arraylist? Warum diese JSONException?

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

und ich brauche die IDs in einem ArrayList oder einer einfachen Array zu speichern.

Hinweis: Die Anzahl der IDs kann variieren.

Das Problem ist, als ich leicht "TOTAL_COUNT" mit

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

bekommen kann, aber wenn ich versuche, jede "id" zu bekommen, wenn ich rufe

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

ich immer ein JSONException bekommen

Dies ist mein Fehlercode

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(); 
+0

"Daten" ist ein Array. Versuchen Sie es mit 'JSONArray data = listatodoslosamigos.getJSONObject (" data ");' –

Antwort

0

Ihre data Eigenschaft im JSON ein Array ist, so müssen Sie getJSONArray, zum Beispiel verwenden;

JSONArray data = listatodoslosamigos.getJSONArray("data"); 

Sie können für eine JSONArray iterator .listIterator() oder über .getJSONObject(index) verwendet wird;

JSONArray data = listatodoslosamigos.getJSONArray("data"); 
JSONObject member = data.getJSONObject(0); 

Sie können auch JSONArray.toCollection und data.toArray verwenden, um eine JSONArray in eine normale Java-Sammlung oder Array zu drehen.

0

Ich glaube, Sie wollen:

JSONArray data = listatodoslosamigos.getJSONArray("data"); 
0

Diese Zeile ist falsch JSONObject data = listatodoslosamigos.getJSONObject ("data");

Replace this line with below line 

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

B'caz data is json array type and you are trying to get json object so that use found this type of exception. 
Verwandte Themen