2012-03-19 3 views
5

Aggiornamento: Ho appena testato il mio formato JSON restituito dal server utilizzando JSONlint ed è corretto.errore selettore non riconosciuto con NSJSONSerialization + AFNetworking

Ricevo un'eccezione con NSJSONSerialization in una chiamata AFNetworking a uno script php che restituisce dati JSON. Ho esaminato le altre domande qui con lo stesso problema e ho provato quelle soluzioni, ma sto ancora ricevendo l'errore.

Si blocca su questa linea:

NSError *e = nil; 
NSMutableArray *jsonArray = 
[NSJSONSerialization JSONObjectWithData: jsonData 
           options: NSJSONReadingMutableContainers 
            error: &e]; 

Registro errori:

2012-03-19 18: 10: 41,291 ImageUploader [3538: 207] * terminazione app a causa dell'eccezione non rilevata 'NSInvalidArgumentException', motivo: '- [__ NSCFArray bytes]: selettore non riconosciuto inviato all'istanza 0x6867430'

miei dati JSON, quando chiamo lo script php tramite il browser si presenta così:

[{ "utente": "Binky", "percorso": "Binky-0a96f9aab5267c8.jpg"," indice ":" 101 "}, {" utente ":" Binky", "path": "Binky-9cf844252c28553.jpg", "index": "102"}, { "utente": "Binky", "path" : "Binky-d6c749d25d33015.jpg", "indice": "103"}]

la NSLog dei dati seguente aspetto:

( { indice = 101; path = "binky-0a96f9aab5267c8.jpg"; utente = binky; }, { indice = 102; path = "binky-9cf844252c28553.jpg"; utente = binky; }, { indice = 103; path = "binky-d6c749d25d33015.jpg"; utente = binky; })

Infine, fare un test per assicurarsi che ho valida dati JSON:

  if ([NSJSONSerialization isValidJSONObject: jsonData]){ 
      NSLog(@"Good JSON \n"); 
     } 

Quindi non riesco a capire dove la fonte del mio errore è. Piccolo aiuto?

// AFNetworking funzionamento + blocco

AFJSONRequestOperation *operation = 
    [AFJSONRequestOperation JSONRequestOperationWithRequest:myRequest 
    success:^(NSURLRequest *request, NSHTTPURLResponse *response, id jsonData) { 

     NSLog(@"Success JSON data:\n %@ \n", jsonData); //log data 

     if ([NSJSONSerialization isValidJSONObject: jsonData]){ 
      NSLog(@"Good JSON \n"); 
     } 

     NSError *e = nil; 
     NSMutableArray *jsonArray = [NSJSONSerialization JSONObjectWithData: jsonData options: NSJSONReadingMutableContainers error: &e]; 

     if (!jsonArray) { 
      NSLog(@"Error parsing JSON: %@", e); 
     } else { 
      for(NSDictionary *item in jsonArray) { 
       NSLog(@"Item: %@", item); 
      } 
     } 

     [self.navigationController popToRootViewControllerAnimated:YES]; 
    } 
    failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) { 

     NSLog(@"Error: %@", error); 
     [self.navigationController popToRootViewControllerAnimated:YES]; 
    }]; 

risposta

7

Aparently JSONObjectWithData attende NSData anziché una matrice. id jsonData sembra essere una matrice che rappresenta il contenuto della stringa json. A parte te, comunque, ti stai aspettando una matrice.

Per qualche motivo lo stai facendo due volte.Invece di

NSMutableArray *jsonArray = [NSJSONSerialization JSONObjectWithData: jsonData options: NSJSONReadingMutableContainers error: &e]; 

si può semplicemente utilizzare

NSMutableArray *jsonArray = [NSMutableArray arrayWithArray:jsonData]; 

o, se non deve essere mutabile:

NSArray *jsonArray = (NSArray *) jsonData; 

Tuttavia, si dovrebbe sempre verificare se è veramente una matrice in jsonData. A seconda della struttura all'interno della stringa json potrebbe essere un NSDictionary o nullo in caso di errori.

+0

grazie. Convertire il jsondata in ingresso in un 'NSMutableArray' funziona, ma non capisco perché avrei bisogno di' NSJSONSerialization' qui (è usato in un sacco di codice di esempio che è proprio come il mio). –