我正在尝试解析来自AFNetworking的响应对象。我能够将 responseObject 的结果存储到一个名为 getData 的 NSDictionary 对象中,但我使用断点对其进行了测试,getData 只包含一堆字符串。有人知道我如何提取数据吗?
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;
}];
下面是 getData 中字符串的外观图像。
从响应对象获取数据后获取数据的内容
reponseObject 是一个 id,所以它最终将是一个 NSDictionnary。你可以这样解析它:
YourVariable = [responseObject objectForKey:(@"yourKey")];
尝试使用,
getdata = [[NSDictionary alloc] initWithDictionary: responseObject];
然后检查。
谢谢大家的帮助。我终于弄清楚了发生了什么。此方法是从外部数据库下载文件。每个文件都有一个 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 调用,我现在可以获取该文件的数据。