无法将JSON解析为UITableView

时间:2014-10-28 04:28:28

标签: objective-c uitableview nsdictionary afnetworking afnetworking-2

目前我正在尝试解析responseObject来填充我的表视图,但是我在使用responseObject解析数组时遇到了一些问题。我想用responseObject[@"test"]的值填充我的单元格。

请求操作:

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;
}

allMembers的类型为NSArray,而member的类型为NSDictionary。 当我尝试记录member的值时,它返回null。它出了什么问题?

1 个答案:

答案 0 :(得分:-1)

你应该试试这个

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