0

I am trying to parse a responseObject from AFNetworking. I was able to store the responseObject's results into a NSDictionary object called getData but I tested it out using a breakpoint, and getData just contains a bunch of strings. Does anybody know how I can extract the data?

manager.responseSerializer = [AFHTTPResponseSerializer serializer];
NSSet *acceptableTypes = [NSSet setWithObjects:@"application/json", @"text/plain", nil];
manager.responseSerializer.acceptableContentTypes = acceptableTypes;

__block NSDictionary *getData;
__block NSMutableArray *filenames = [[NSMutableArray alloc] init];

[manager GET:URLString parameters:parameters
     success:^(AFHTTPRequestOperation *operation, id responseObject)
 {
     NSLog(@"responseObject: %@", responseObject);
     NSLog(@"operation.responseString: %@",operation.responseString);
     NSLog(@"operation.response: %@",operation.response);
     self.downloadSuccess = YES;

     getData = [NSJSONSerialization JSONObjectWithData:responseObject options:0 error:nil];
     NSLog(@"size: %lu", (unsigned long)getData.count);

 }
     failure:^(AFHTTPRequestOperation *operation, NSError *error)
 {
     NSLog(@"Error [getDataFromServer]: %@", error);
     NSLog(@"Error Response --> %@",operation.responseString);
     self.downloadSuccess = NO;
 }];

Here is an image of what the strings in getData look like.

Content of getData after getting data from responseObject

km786
  • 65
  • 1
  • 1
  • 5
  • Usually if your server sets the right header, then responseObject should be decoded JSON, therefore it's NSArray or NSDictionary. – pronebird Mar 04 '16 at 18:04
  • What kind of data are you trying to get out of the strings? – dan Mar 04 '16 at 19:05
  • you can directly access response object as dictionary as afnetworking already parsed. 'http://stackoverflow.com/a/35723726/3463712' check answer you may get idea. – Max Mar 05 '16 at 07:29
  • I am working on an app where people can create files in the app and then upload them to the external database and also be able to download them. This method basically gets all the content stored for a user from the server and I want to extract all the filenames and put them in a list. – km786 Mar 05 '16 at 20:11

3 Answers3

0

reponseObject is an id, so that will finally be a NSDictionnary. You can parse it like that:

YourVariable = [responseObject objectForKey:(@"yourKey")];
  • I tried doing that by using my NSMutableArray object called filenames like this: filenames = [responseObject objectForKey:@"filename"]; but I am getting an error. It says the responseObject contains NSInlineData and contains 0 key/value pairs and how the selector "objectForKey" is unrecognized for NSInlineData – km786 Mar 05 '16 at 20:22
  • just an idea but try to use NSDictionnary instead of NSMutableDictionnary and tell me the result. edit: OK I just saw you already try a nsdictionnary my bad – Antoine Simon Mar 08 '16 at 11:20
  • Can you give us the print of response object ? place a debug label ( the blue tag when you click at the left of a line) and try to check in the debugger what is responseObject and whit what is it fill. – Antoine Simon Mar 08 '16 at 11:24
  • If you look at the screenshot I attached, right above "getData", you can see "responseObject" (the top half is cut off). It says that responseObject is an NSInlineData and has 0 key/value pairs. – km786 Mar 09 '16 at 17:47
  • can you cast it? like "success:^(AFHTTPRequestOperation *operation, id (NSDictionnary *) responseObject)" – Antoine Simon Mar 10 '16 at 18:06
0

try to use,

getdata = [[NSDictionary alloc] initWithDictionary: responseObject];

and then check.

JigneshP
  • 344
  • 2
  • 7
  • I tried out your suggestion and tested it but I am getting this error: 'NSInvalidArgumentException', reason: '*** -[NSDictionary initWithDictionary:copyItems:]: dictionary argument is not an NSDictionary'. It also says that responseObject is a NSInlineData and getData is nil – km786 Mar 05 '16 at 20:30
  • try to typecast the responseObject in NSDictionary like data = [[NSDictionary alloc] initWithDictionary: (NSDictionary *)responseObject]; – JigneshP Mar 07 '16 at 04:21
0

Thank you everyone for all your help. I finally figured out what is going. This method is to download files from an external database. Each file has an id. The list of weird string I was getting (that I showed in the screenshot) was actually a list of ids for all the files that were uploaded by the user. In order to get the data from a specific file, I need to make a second GET call using that specific id as a parameter. In the code I provided I didn't show how the list of parameters. Here is the list of parameters that I use for the first GET call:

NSDictionary *parameters = @{@"username" : login.username, @"password" :     login.password};

Here is the list of parameters that I use for the second GET call:

NSDictionary *parameters = @{@"username" : login.username, @"password" : login.password, @"id" : @"20160310113730268IDO"};

With the second GET call using that id, i can now get the data of that file.

km786
  • 65
  • 1
  • 1
  • 5