8

I have problems with some public json services with serveices formatted this way

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

NSJSONSerialization seems to be unable to make its work

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);
GAVELLO
  • 123
  • 1
  • 2
  • 4

4 Answers4

17

Using the latest web API I have solved my problem.

For reference, the old web API is: http://api.flickr.com/services/feeds/photos_public.gne?id=xxxxxxxx&lang=en-us&format=json

The current web API is: http://api.flickr.com/services/feeds/photos_public.gne?id=xxxxxxxx&lang=en-us&format=json&nojsoncallback=1

It's done.

Hiren Patel
  • 52,124
  • 21
  • 173
  • 151
5

I think that NSJSONSerialization can't deal with the jsonFlickrFeed ( ... ) in your JSON code. The problem is that the pure response is invalid JSON (test it here).

So you will have to build a way around this issue. You could for example search for the string jsonFlickrFeed( in the response and delete it or - the easier way - just cut off the beginning until the valid JSON starts and cut off the last character (which should be the bracket).

Paul
  • 1,295
  • 2
  • 11
  • 26
3

Flickr does some bad things with their JSON that the parser can't cope with:

  • they start with 'jsonFlickrFeed(' and end with ')'
    • this means that the root object is not valid
  • they incorrectly escape single quotes .. eg. \'
    • this is invalid JSON and will make the parser sad!

For those looking to process the Flick JSON feed

//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 to the story - Flickr is naughty - smack.

So Over It
  • 3,668
  • 3
  • 35
  • 46
0

The problem is that Flickr is returning data in jsonp(JSON with Padding) format instead of json format so to solve this issue just add following to the Flickr URL:

nojsoncallback=1

So if the URL is :

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

then change it to :

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

That's all.

Prateek Gupta
  • 2,422
  • 2
  • 16
  • 30