Valeur non valide autour du caractère 0, NSJSONSerialization

Je fais un get de mon server et j'obtiens une réponse valide:

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { // Append the new data to the instance variable you declared //[_responseData appendData:data]; NSSsortingng *responseBody = [[NSSsortingng alloc] initWithData:data encoding:NSUTF8SsortingngEncoding]; NSLog(@"%@",responseBody); if(data != NULL) { dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, (unsigned long)NULL), ^(void) { NSError *error = nil; //NSMutableArray *jsonArray = [[CJSONDeserializer deserializer] deserializeAsArray:[responseBody dataUsingEncoding:NSUTF8SsortingngEncoding] error:&error]; NSMutableArray *jsonArray = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&error]; if (error) { NSLog(@"JSONObjectWithData error: %@", error); [delegate onErrorGetArrayFromServer]; } else [self parseJSON:jsonArray]; }); } else { if([delegate respondsToSelector:@selector(onErrorGetArrayFromServer)]) { [delegate onErrorGetArrayFromServer]; } } } 

La réponse est comme:

  [{"id":"37", "id_estado":"1", "id_categoria":"1", "nombre":"fer", "email":"[email protected]", "fecha":"2014-07-16 11:25:00", "observaciones":"as dasd asdasd sasd", "latitud":"37.619636", "longitud":"-4.318449", "foto":"images\/default.jpg"}, {"id":"36", "id_estado":"1", "id_categoria":"6", "nombre":"Fernando", "email":"", "fecha":"2014-07-16 10:32:45", "observaciones":"que", "latitud":"37.6178690439634", "longitud":"-4.3238141387701", "foto":"images\/default.jpg"}] 

Cela me jette l'erreur:

 JSONObjectWithData error: Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn't be completed. (Cocoa error 3840.)" (Invalid value around character 0.) UserInfo=0x9e0f610 {NSDebugDescription=Invalid value around character 0.} 

J'ai essayé avec l'autre bibliothèque ( CJSON ) et il m'a jeté l'erreur:

 JSONObjectWithData error: Error Domain=kJSONScannerErrorDomain Code=-202 "Could not scan array. Could not scan a value." UserInfo=0xa15c0e0 {snippet=!HERE>![{"id":"37","id_esta, location=0, NSLocalizedDescription=Could not scan array. Could not scan a value., character=0, line=0} 

Mon server est un server REST et pour mon application Android fonctionne bien.


_SOLVED_

Merci à @Himanshu Joshi:

Pourquoi parsingz-vous datatables dans didReceiveData 😕 Les données ne sont pas téléchargées complètement là, vous devez y append datatables. Analyser datatables dans connectionDidFinishLoading: méthode déléguée –

J'ai analysé datatables dans connectionDidFinishLoading: et tout va bien.

J'ai rencontré le même problème, mais c'était parce que mon URL initiale n'était pas correcte.

La réponse doit être vérifiée avant d'parsingr le JSON. Cela pourrait vous dire que ça n'a pas été trouvé.

À ce stade, datatables peuvent effectivement contenir une page HTML 404.

J'utilise ces methods pour déboguer cela, et des methods plus simples pour traiter la requête web:

 // Asynchronous fetch the JSON and call the block when done NSURLSessionDataTask *downloadTask = [[NSURLSession sharedSession] dataTaskWithURL:url completionHandler: ^(NSData *data, NSURLResponse *response, NSError *error) { NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *) response; if ([httpResponse statusCode]!=200) { // log [response description] // log [NSSsortingng ssortingngWithUTF8Ssortingng:[data bytes]] return; } NSDictionary* jsonDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&error]; if (!jsonDic) { return; } // process the JSON }]; [downloadTask resume]; 

Votre JSON a un espace avant datatables:

  [{"id":"37", 

Vérifiez votre server.