我是新來解析目標C中的json文件,我想從列表中解析Twitter json訂閱用戶名。 我想獲取用戶的關注者列表,並解析他們以獲取user_ids,然後再次調用另一個URL來獲取他們的姓名和個人資料圖片。 我得到的人的名字,但我無法正確解析列表。 如果有人能幫助我,這將會非常有幫助。 我對獲取數據代碼:添加數組到單元行來自Twitter客戶端訂閱iphone
-(void) fetchData{
ACAccountStore *account = [[ACAccountStore alloc] init];
ACAccountType *accountType = [account accountTypeWithAccountTypeIdentifier:ACAccountTypeIdentifierTwitter];
[account requestAccessToAccountsWithType:accountType withCompletionHandler:^(BOOL granted, NSError *error){
if (granted == YES) {
NSArray *arrayOfAccounts = [account accountsWithAccountType:accountType];
if ([arrayOfAccounts count] > 0) {
ACAccount *acct = [arrayOfAccounts objectAtIndex:0];
NSString *username = acct.username;
NSLog(@"Account : %@", username);
TWRequest *fetchFriendsFollowers = [[TWRequest alloc]
initWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"https://api.twitter.com/1/followers/ids.json?screen_name=%@",acct.username]]
parameters:nil
requestMethod:TWRequestMethodGET];
[fetchFriendsFollowers performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error){
if ([urlResponse statusCode] == 200) {
dispatch_async(dispatch_get_main_queue(), ^{
NSError *jsonParsingError = nil;
NSDictionary *response = [NSJSONSerialization JSONObjectWithData:responseData options:0 error:&jsonParsingError];
self.responseArray = [response objectForKey:@"ids"];
for (int i =0 ; i < [self.responseArray count]; i++) {
NSString *user_id = [self.responseArray objectAtIndex:i];
TWRequest *fetchFriendsFollowersNames = [[TWRequest alloc]
initWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"https://api.twitter.com/1/users/lookup.json?user_id=%@",user_id]]
parameters:nil
requestMethod:TWRequestMethodPOST];
[fetchFriendsFollowersNames performRequestWithHandler:^(NSData *responseData, NSHTTPURLResponse *urlResponse, NSError *error){
if ([urlResponse statusCode] == 200) {
dispatch_async(dispatch_get_main_queue(), ^{
NSError *jsonParsingError = nil;
NSArray *response = [NSJSONSerialization JSONObjectWithData:responseData options:0 error:&jsonParsingError];
for (NSDictionary *user in response) {
[self.userNameArray addObject:[user objectForKey:@"name"]];
[self.tableView reloadData];
}
});
}
}];
}
NSLog(@"responseArray %@ for user: %@",self.responseArray,username);
});
}
}];
}
else{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error"
message:@"Please add twitter accounts on your phone and log back in."
delegate:nil
cancelButtonTitle:@"Ok"
otherButtonTitles: nil];
[alert show];
}
}
}];
}
然後我在的cellForRowAtIndexPath顯示它的所有用戶名列表。它實際上獲得列表並在所有單元格中重複該名稱。我知道我正在做一些愚蠢的錯誤,但無法弄清楚,因爲我一直在看這一段時間,無法得到它的修復。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
for (int i=0; i < [self.userNameArray count]; i++) {
NSLog(@"Text : %@", [self.userNameArray objectAtIndex:i]);
cell.textLabel.text = [self.userNameArray objectAtIndex:i];
}
return cell;
}
不需要for循環:cell.textLabel.text = [self.userNameArray objectAtIndex:indexPath.row]; – 2012-03-18 23:08:19