如何从服务器解析数组答案

时间:2016-08-12 14:30:23

标签: ios objective-c

这是我从API获得的答案:

enter image description here

- (void)postRequestWithHeader:(NSString *)url
     parameters:(NSDictionary *)parameters
     completionHandler:(void (^)(NSDictionary *data, NSError *))completionHandler
    {
NSString *URLString = [NSString stringWithFormat:@"%@%@", baseURL, url];


NSMutableURLRequest *request= [[AFHTTPRequestSerializer serializer] requestWithMethod:@"POST" URLString:URLString parameters:parameters error:nil];
  NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];

NSString *session =[[LocalDataBase sharedDataBase]userDataObjectForKey:@"session"];
[request setValue:session forHTTPHeaderField:@"session"];


NSURLSessionDataTask *dataTask = [manager dataTaskWithRequest:request
                                            completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
                                                NSDictionary *backParam = [[AFHTTPResponseSerializer serializer] responseObjectForResponse:responseObject data:responseObject error:nil];

                                                if (error) {
                                                    NSLog(@"Error: %@", error);
                                                    completionHandler(nil, error);
                                                } else {
                                                    if ([responseObject isKindOfClass:[NSArray class]]) {
                                                        NSLog(@"response is in array");
                                                                                                                     }else {
                                                    NSLog(@"%@ %@", response, responseObject);
                                                    completionHandler(backParam, nil);
                                                    }
                                                    ;}
                                            }];
[dataTask resume];



    }


-(void) requestForPosts:(void (^)(NSError *error))completionHandler
   {

[self postRequestWithHeader:@"posts/list"
             parameters:nil
      completionHandler:^(NSDictionary *data, NSError *error)  {
    if (error) {
        NSLog(@"Error: %@", error);
        completionHandler(error);
    } else {
        NSLog(data);
       // [[LocalDataBase sharedDataBase] saveUserData:data];
        completionHandler(nil);
    }

}];
}

这是我用来发送API请求的代码然后我得到响应并检查它是否是数组,但我不知道在这之后做了什么 我需要得到这个响应并放入表格视图/ 我希望有人会帮助我)

1 个答案:

答案 0 :(得分:0)

您的网络服务为您提供了json响应。

在完成块中,参数数据不是NSDictionary类型,而是NSArray *

[[AFHTTPResponseSerializer serializer] responseObjectForResponse:responseObject data:responseObject error:nil]

已经在NSArray中解析了对象,而不是NSDictionary ...

How do I parse JSON with Objective-C?