Unable to parse json from AFNetworking's responseObject iOS

时间:2016-03-04 17:59:52

标签: ios objective-c json parsing afnetworking

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

3 个答案:

答案 0 :(得分:0)

reponseObject是一个id,因此最终将成为一个NSDictionnary。 你可以这样解析它:

YourVariable = [responseObject objectForKey:(@"yourKey")];

答案 1 :(得分:0)

尝试使用,

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

然后检查。

答案 2 :(得分:0)

感谢大家的帮助。我终于弄清楚到底发生了什么。此方法是从外部数据库下载文件。每个文件都有一个id。我收到的奇怪字符串列表(我在屏幕截图中显示)实际上是用户上传的所有文件的ID列表。为了从特定文件中获取数据,我需要使用该特定id作为参数进行第二次GET调用。在我提供的代码中,我没有显示参数列表。以下是我用于第一次GET调用的参数列表:

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

以下是我用于第二次GET调用的参数列表:

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

使用该id进行第二次GET调用,我现在可以获取该文件的数据。