0

目前我試圖解析responseObject來填充我的表格視圖,但我一直在解析響應對象中的數組有問題。我想用來自responseObject[@"test"]的值填充我的單元格。無法解析JSON到UITableView

請求操作:

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; 
[manager POST:[kBaseURL stringByAppendingString:@"directori/member_list"]parameters:params 
     success:^(AFHTTPRequestOperation *operation, id responseObject) { 
      NSLog(@"RESPONSE: %@", responseObject); 
      if ([responseObject[@"status_reason"] isEqualToString:@"Success"]) { 
       self.allMembers = responseObject[@"test"]; 
       NSLog(@"MEMBERS: %@", self.allMembers); 
      } else { 
       // handle error 
      } 
     } failure:^(AFHTTPRequestOperation *operation, NSError *error) { 
      NSLog(@"ERROR: %@", error); 
     }]; 

正如下面可以看到,test是對象的數組。

樣品responseObject:

RESPONSE: { 
    test =  (
       { 
      img = "http://test.com/assets_api/images/no_image.png"; 
      location = "test location"; 
      name = "test user"; 
      position = "test"; 
     } 
    ); 
    "audit_no" = ""; 
    "status_code" = 200; 
    "status_reason" = Success; 
} 

而這裏的是我的表視圖的數據源。

表視圖數據源

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"DirectoryCell"; 
    DirectoryViewCell *cell = (DirectoryViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 

    if (cell==nil) { 
     cell = [[DirectoryViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; 
    } 

    NSDictionary *member = [self.allMembers objectAtIndex:indexPath.row]; 
    NSLog(@"MEMBER: %@", member); 
    return cell; 
} 

allMembersNSArray類型而memberNSDictionary類型。 當我嘗試記錄member的值時,它返回null。它出了什麼問題?

回答

-1

您應該試試這個

NSDictionary *member = [[self.allMembers objectAtIndex:indexPath.row]objectAtIndex:0];