J'ai des problèmes avec certains services JSON publiques Avec des services formatés de cette manière
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);
4 Réponses :
Je pense que NsjsonSerialization ne peut pas gérer le jsonflickrfeed (...) em> dans votre code JSON. Le problème est que la réponse pure est invalide JSON (test IT ici ). p>
Vous devrez donc construire une solution autour de cette question. Vous pouvez par exemple rechercher la chaîne jsonflickrfeed ( em> dans la réponse et supprimez-la ou - le moyen plus facile - il suffit de couper le début jusqu'à ce que le JSON valide commence et coupé le dernier caractère (ce qui devrait être le support). P>
Flickr fait quelques mauvaises choses avec leur Json que l'analyseur ne peut pas faire face à:
Pour ceux qui cherchent à traiter le flick JSON Feed P>
//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);
}
Utilisation de la dernière API Web que j'ai résolu mon problème. P>
Pour référence, l'API Wi-Wi-Web L'API de Web actuel forte> est:
http://api.flickr.com /Services/feeds/photos_public.gne?id=xxxxxxxx&lang=en-us&format=json&nojsoncallback=1 P>
Le problème est que Flickr renvoie des données dans JSONP (JSON With Padding) au lieu du format JSON afin de résoudre ce problème simplement Ajouter après la suite de l'URL Flickr: P>
Donc, si l'URL est: p>
https://www.flickr.com/services/feeds/photos_public.gne?format=json P>
blockQuote>
puis changez-le à: p>
https://www.flickr.com/services/feeds/photos_public.gne?format=json&nojsoncallback=1 p>
blockQuote>
C'est tout. P> nojsoncallback = 1 code> p>