1

I am using flickr public API, Where I am facing issue with parsing of JSON. Here is my code.

 NSString *urlString = @"https://api.flickr.com/services/feeds/photos_public.gne?format=json";
    NSURL *url = [NSURL URLWithString:urlString];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];

    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
   // operation.responseSerializer = [AFJSONResponseSerializer serializer];

    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {

        //(JSON text did not start with array or object and option to allow fragments not set.)
        NSString *rawString = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];

        NSData *refinedData = [responseObject subdataWithRange:NSMakeRange([@"jsonFlickrFeed(" length], [responseObject length]-([@"jsonFlickrFeed(" length]+1))];
        NSError *parseError = nil;

        NSDictionary *jsonObject=[NSJSONSerialization
                                  JSONObjectWithData:refinedData
                                  options:NSJSONReadingMutableLeaves
                                  error:&parseError];
        NSError *parsingError = nil;



    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {


    }];

    [operation start];

Error while parsing:

Error Domain=NSCocoaErrorDomain Code=3840 "The data couldn’t be read because it isn’t in the correct format." (Invalid escape sequence around character 15901.) UserInfo=0x7fa613da71c0 {NSDebugDescription=Invalid escape sequence around character 15901.}

Is there any specific character set should i need to specify while parsing?

What ever the changes required that needs to be done at my end since the API response is not in my control.

Solution: @vishnuvaran's guess logically correct, Apart from that we need to escape "\'" this character then only I can able to parse the JSON.

[requiredString replaceOccurrencesOfString:@"\\\'"  withString:@" "   options:NSLiteralSearch range:NSMakeRange(0, [requiredString length])];
Wyetro
  • 8,439
  • 9
  • 46
  • 64
ajay
  • 3,245
  • 4
  • 31
  • 59

1 Answers1

1

You are giving wrong sub data range.Just change this line

NSData *refinedData = [responseObject subdataWithRange:NSMakeRange([@"jsonFlickrFeed(" length], [responseObject length]-([@"jsonFlickrFeed(" length]+1))];

to below line

NSData *refinedData = [responseObject subdataWithRange:NSMakeRange([@"jsonFlickrFeed(" length], ([responseObject length]-1)-([@"jsonFlickrFeed(" length]))];

Hope it will help you.

Vishnuvardhan
  • 5,081
  • 1
  • 17
  • 33
  • That too i did not change any thing apart of the above line. – Vishnuvardhan Aug 20 '15 at 11:31
  • @Vishnuvardhan Its dynamic data, Each time that url sends different results. I tried your logic some times it is working and some times it is not working. May be because of some sort of characters preventing us the parsing. Is there any specific encoding needs to use? – ajay Aug 20 '15 at 14:41
  • @ajay I don't think so it's because of encoding.I totally believe it's caused due to subdata range. – Vishnuvardhan Aug 21 '15 at 03:58