2011-12-30 27 views
5

tengo problemas con algunos servicios públicos JSON con serveices con formato de esta maneraNSJSONSerialization

jsonFlickrFeed({ 
     "title": "Uploads from everyone", 
     "link": "http://www.flickr.com/photos/", 
     "description": "", 
     "modifi ... }) 

NSJSONSerialization parece ser incapaz de hacer su trabajo

NSURL *jsonUrl = [NSURL URLWithString:@"http://d.yimg.com/autoc.finance.yahoo.com/autoc?query=yahoo&callback=YAHOO.Finance.SymbolSuggest.ssCallback"]; 
NSError *error = nil; 
NSData *jsonData = [NSData dataWithContentsOfURL:jsonUrl options:kNilOptions error:&error]; 
NSMutableDictionary *jsonResponse = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingAllowFragments error:&error]; 
NSLog(@"%@", jsonResponse); 

Respuesta

5

Creo que NSJSONSerialization no puede ocuparse de jsonFlickrFeed (...) en su código JSON. El problema es que la respuesta pura es JSON no válida (pruébelo here).

Así que tendrás que solucionar el problema. Por ejemplo, puede buscar la cadena jsonFlickrFeed ( en la respuesta y eliminarla o, de manera más sencilla, simplemente cortar el inicio hasta que comience el JSON válido y cortar el último carácter (que debería ser el paréntesis).

3

Flickr hace algunas cosas malas con su JSON que el analizador no puede hacer frente a:

  • que empiezan con 'jsonFlickrFeed (' y terminan con ')'
    • esto significa que el objeto raíz no es válido
  • escapan de forma incorrecta las comillas simples .. ej. \ '
    • esto es JSON no válido y hará que el analizador sea triste!

Para aquellos que buscan para procesar la Flick JSON alimentar

//get the feed 
NSURL *flickrFeedURL = [NSURL URLWithString:@"http://api.flickr.com/services/feeds/photos_public.gne?format=json&tags=data"]; 
NSData *badJSON = [NSData dataWithContentsOfURL:flickrFeedURL]; 
//convert to UTF8 encoded string so that we can manipulate the 'badness' out of Flickr's feed 
NSString *dataAsString = [NSString stringWithUTF8String:[badJSON bytes]]; 
//remove the leading 'jsonFlickrFeed(' and trailing ')' from the response data so we are left with a dictionary root object 
NSString *correctedJSONString = [NSString stringWithString:[dataAsString substringWithRange:NSMakeRange (15, dataAsString.length-15-1)]]; 
//Flickr incorrectly tries to escape single quotes - this is invalid JSON (see http://stackoverflow.com/a/2275428/423565) 
//correct by removing escape slash (note NSString also uses \ as escape character - thus we need to use \\) 
correctedJSONString = [correctedJSONString stringByReplacingOccurrencesOfString:@"\\'" withString:@"'"]; 
//re-encode the now correct string representation of JSON back to a NSData object which can be parsed by NSJSONSerialization 
NSData *correctedData = [correctedJSONString dataUsingEncoding:NSUTF8StringEncoding]; 
NSError *error = nil; 
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:correctedData options:NSJSONReadingAllowFragments error:&error]; 
if (error) { 
    NSLog(@"this still sucks - and we failed"); 
} else { 
    NSLog(@"we successfully parsed the flickr 'JSON' feed: %@", json); 
} 

Moral de la historia - Flickr es traviesa - justo.

Cuestiones relacionadas