Search code examples
iosiphoneafnetworking-2

Parse JSON file using AFNetworking


I have a link which contains the a json file. I mean if I launch that link in chrome a file is downloaded on my computer which is of .json extension. say the link is www.foffa.com/sampleList.json I am new to AFNetworking and not sure how to parse this json, with or without having to write this file down to the disk.

My code looks like below and I am pretty sure that I have to use streams and all for this but I am not sure how. As of now I get an error "Request failed: unacceptable content-type: text/plain" I guess it expects the content type to be "Content-Type" = "application/octet-stream";

    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
    operation.responseSerializer = [AFHTTPResponseSerializer serializer];
    operation.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"application/octet-stream"];
    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
             NSLog(@"Data retrived");       
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {

        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error Retrieving Weather"
                                                            message:[error localizedDescription]
                                                           delegate:nil
                                                  cancelButtonTitle:@"Ok"
                                                  otherButtonTitles:nil];
        [alertView show];
    }];

Solution

  • Change the second line in this:

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

    to this:

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

    Note you are using the AFJSONResponseSerializer response serialised.

    Update

    Updating my answer from info in the below comments, it appears you actually want to download a JSON file from a server and then parse it as opposed to parsing JSON directly from the HTTP response:

    In order to do that change the content type to text/plain and with the downloaded file/data parse either as a String or a JSON object like this:

    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:nil];
    operation.responseSerializer = [AFHTTPResponseSerializer serializer];
    operation.responseSerializer.acceptableContentTypes =
        [NSSet setWithObjects:@"application/octet-stream", @"text/plain", nil];
    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
      NSError *error;
      id json = [NSJSONSerialization JSONObjectWithData:responseObject options:0 error:&error];
      if (error) {
          NSLog(@"Error: %@", error);
      } else {
          NSLog(@"JSON: %@", json);
      }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    
      UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error Retrieving Weather"
                                                          message:[error localizedDescription]
                                                         delegate:nil
                                                cancelButtonTitle:@"Ok"
                                                otherButtonTitles:nil];
      [alertView show];
    }];