2012-01-22 22 views
7

mia stringa JSON si presenta così (contenuta in una variabile stringa denominata sJSON):Come eseguire iterazioni su questo array JSON utilizzando Java e org.json in Android?

[ 
    {"id":284} 
], 
[ 
    {"name":John,"surname":Doe}, 
    {"name":Jane,"surname":Doe} 
] 

sono in grado di analizzare il primo array come questo (utilizzando Java e l'importazione di org.json):

JSONArray arrJSON = new JSONArray(sJSON); 
JSONObject jsonGeneralData = new JSONObject(arrJSON.get(0).toString()); 

String sResult = jsonGeneralData.get("id").toString(); 

Ciò restituisce il risultato previsto, che è 284. Non riesco a ottenere la seconda serie di elementi e a scorrere tra di loro. Non sono sicuro che la mia stringa JSON non sia corretta, o se sto cercando di accedervi nel modo sbagliato. Ecco quello che ho provato:

JSONObject jsonPersonData = new JSONObject(arrJSON.get(1).toString()); 

Questo è quanto ho ottenuto, non riesco a capire come un ciclo tra le singole voci all'interno del secondo array.

EDIT:

Sembra che questa linea analizza solo la prima stringa nelle parentesi quadre:

JSONArray arrJSON = new JSONArray(sJSON); 

O il JSON è sbagliato (stesso esempio come sopra), o non è parsing è correttamente? Sono riuscito a risolvere il problema eseguendo una divisione sulla stringa e inserendoli ciascuno nella propria JSONArray, ma non penso che sia il modo migliore di fare le cose.

+0

aggiornato il post originale con più informazioni –

+0

Uno ricorsivo: https://gist.github.com/anonymous/89f8163fb22f77f4f9d3 –

risposta

20

si desidera qualcosa di simile ..

JSONArray jsonArray = new JSONArray(sJSON); 
JSONArray jsonPersonData = jsonArray.getJSONArray(1); 
for (int i=0; i<jsonPersonData.length(); i++) { 
    JSONObject item = jsonPersonData.getJSONObject(i); 
    String name = item.getString("name"); 
    String surname = item.getString("surname"); 
} 
3

È necessario accedere ai dati utilizzando l'oggetto arrJson, non creando nuovi JSONObjects con i relativi risultati.

5

Non shoul utilizzare JSONObject.toString. Questo è come si dovrebbe scorrere la matrice:

for(int i=0;i<arrJSON.length();i++){ 
JSONObject json_data = jArray.getJSONObject(i); 
String name =json_data.getString("name"); 
String surname =json_data.getString("surname"); 
} 
2

È possibile scorrere utilizzando un Iterator, ho anche copiato una stringa di esempio JSON per te, spero che questo può aiutare:

// { "drives": 
    //[{"drives_id":"2","ip":"hq10m.backupexample.com","port":"4010","ftp_username":"NameSFTP"," 
    // ftp_password":"12345","permission":2,"drive_name":"dev" 
    //,"drive_letter":"S","size":"1234","ma_mode":"0"}, 
    //{"drives_id":"3","ip":"hq6m.backupexample.com","port":"4206","ftp_username":"userSFTP" 
//,"ftp_password":"456","permission":2,"drive_name":"Rashtesting" 
//,"drive_letter":"P","size":"8","ma_mode":"0"}],"user_id":"8"} 

     mi.setUser_id((int) Long.parseLong(obj.get("user_id").toString())); 
     JSONArray drivesarray= (JSONArray) obj.get("drives"); 
     Iterator<JSONObject> driveIterator=drivesarray.iterator(); 
     while(driveIterator.hasNext()) 
     { 
      JSONObject driveJSON=driveIterator.next(); 
      client_drive_info drive=new client_drive_info(); 
      drive.setDrives_id((int) Long.parseLong(driveJSON.get("drives_id").toString())); 
      drive.setIp(driveJSON.get("ip").toString()); 
      drive.setPort((int)Long.parseLong(driveJSON.get("port").toString())); 
      drive.setSFTPusername(driveJSON.get("ftp_username").toString()); 
      drive.setSFTPpassword(driveJSON.get("ftp_password").toString()); 
      drive.setPermission((int)Long.parseLong(driveJSON.get("permission").toString())); 
      drive.setDrive_name(driveJSON.get("drive_name").toString()); 
      drive.setDrive_letter(driveJSON.get("drive_letter").toString()); 
      drive.setSize((int)Long.parseLong(driveJSON.get("size").toString())); 

      mi.getDrives_info().add(drive); 
     } 
+0

Si potrebbe prendere in considerazione l'uso di 'JSONObject.getString()'. – deed02392

Problemi correlati