Ankit Srivastava
Ankit Srivastava

Reputation: 12405

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

Upvotes: 1

Views: 1147

Answers (2)

sbarow
sbarow

Reputation: 2819

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

Upvotes: 2

Sergii Martynenko Jr
Sergii Martynenko Jr

Reputation: 1407

AFNetwroking won't parse income data for you.

Use NSJSONSerialization's class method +JSONObjectWithData:options:error:. If your data is valid, result will be NSDictionary with same structure as original JSON file

You should place it inside success block

Upvotes: 0

Related Questions