8
votes

Nsjsonsorialisation

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);


0 commentaires

4 Réponses :


5
votes

Je pense que NsjsonSerialization ne peut pas gérer le jsonflickrfeed (...) dans votre code JSON. Le problème est que la réponse pure est invalide JSON (test IT ici ).

Vous devrez donc construire une solution autour de cette question. Vous pouvez par exemple rechercher la chaîne jsonflickrfeed ( 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).


0 commentaires

3
votes

Flickr fait quelques mauvaises choses avec leur Json que l'analyseur ne peut pas faire face à:

  • Ils commencent par 'JSONFLICKRFEED (' et fin de ')' '
    • Cela signifie que l'objet racine n'est pas valide li> ul> li>
    • Ils échappent à tort de citations simples. Par exemple. \ '
      • Ceci est invalide JSON et fera l'analyseur SAD! LI> ul> li> ul>

        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);
        }
        


0 commentaires

17
votes

Utilisation de la dernière API Web que j'ai résolu mon problème.

Pour référence, l'API Wi-Wi-Web est: http://api.flickr.com/services /feeds/photos_public.gne?id=xxxxxxxx&lang=en-us&format=json

L'API de Web actuel est: http://api.flickr.com /Services/feeds/photos_public.gne?id=xxxxxxxx&lang=en-us&format=json&nojsoncallback=1

c'est fait .


0 commentaires

0
votes

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:

nojsoncallback = 1

Donc, si l'URL est:

https://www.flickr.com/services/feeds/photos_public.gne?format=json

puis changez-le à:

https://www.flickr.com/services/feeds/photos_public.gne?format=json&nojsoncallback=1

C'est tout.


0 commentaires