私は目的CのjSONファイルを解析するのが初めてで、Twitterのjsonを解析してリストからユーザー名を取得したいと思っています。 私はユーザーのフォロワーのリストを取得し、それらを解析してuser_idsを取得し、再度別のURLを呼び出して名前とプロフィール写真を取得しようとしています。 人の名前を取得していますが、リストを正しく解析できません。 誰かが私をここで助けることができれば、本当に役に立ちます。 データをフェッチするための私のコード:iphoneのTwitterクライアントフィードから配列をCell Rowに追加する
-(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]; –