2012-09-25 4 views
2

내가 ASIhttprequest를 사용하여 this JSON data 내 jQuery과를 채우기 위해 노력하고 있지만, IK는 다음과 같은 예외가 얻을 : __NSArrayM objectForKey:]: unrecognized selector sent to instanceASIHttpRequest JSON 데이터로 UITableView를 채우는 방법은 무엇입니까?

나는이 달성하기 위해 내 컨트롤러에 다음 코드를 사용

#import <UIKit/UIKit.h> 

@interface tableListViewController : UITableViewController { 
    NSDictionary *data; 
} 

@end 

그리고 주를 파일 :

#import "tableListViewController.h" 
#import "ASIHTTPRequest.h" 
#import "ASIFormDataRequest.h" 

@interface tableListViewController() 

@end 

@implementation tableListViewController 

- (id)initWithStyle:(UITableViewStyle)style 
{ 
    self = [super initWithStyle:style]; 
    if (self) { 
     // Custom initialization 
    } 
    return self; 
} 

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    NSURL *url = [NSURL URLWithString:@"http://api.glennzo.nl/glow/index.json"]; 
    ASIHTTPRequest *request = [ASIFormDataRequest requestWithURL:url]; 
    [request setTag:100]; 
    [request setDelegate:self]; 
    [request startSynchronous]; 

    //Set the title 
    self.navigationItem.title = @"Events"; 
} 

- (void)viewDidUnload 
{ 
    [super viewDidUnload]; 
} 

- (void) requestFinished:(ASIHTTPRequest *) request 
{ 
    if (request.tag == 100) { 
     NSData *responseData = [request responseData]; 
     NSError *error; 

     NSDictionary *JSON = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:&error]; 

     if(!JSON || ![JSON count]) 
     { 
      NSLog(@"%@", @"geen correcte json data van request gekregen"); 
     } 
     else 
     { 
      data = JSON; 
     } 
    } 
} 

- (void) requestFailed:(ASIHTTPRequest *) request 
{ 
    NSError *error = [request error]; 
    NSLog(@"Error: %@",error.localizedDescription); 
} 

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation 
{ 
    return (interfaceOrientation == UIInterfaceOrientationPortrait); 
} 

#pragma mark - Table view data source 

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{ 
    // Return the number of sections. 
    return 1; 
} 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 
    // Return the number of rows in the section. 
    return [data count]; 

} 

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell"; 
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; 
    if(cell == nil) 
    { 
     cell = [UITableViewCell alloc]; 
    } 

    NSArray *namen = [data objectForKey:@"name"]; 
    cell.textLabel.text = [namen objectAtIndex:indexPath.row]; 

    return cell; 
} 

감사 디버깅에 내가 지금 ASIHttpRequest가 작동하는 것을 알고 있지만, NSDictionary에 내가 (전체 응용 프로그램의 주위에 사용하는 의도와) 만드는 것이 나에게 그럴를 제공합니다 불용성. 누군가 올바른 방향으로 나를 설 수 있습니까?

[NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:&error]; 

하는보고보십시오 :

NSArray *namen = [data objectForKey:@"name"]; 

그래서 내가 data 유형 (NSDictionary와) 정확하지 전무이거나 다른 유형으로 가득 생각 :

답변

1

이 오류에있을 것 같다 JSON을 통해 무엇이 도착했는지. 예 :

NSError *jsonError = nil; 
id jsonObject = [NSJSONSerialization JSONObjectWithData:jsonData options:kNilOptions error:&jsonError]; 

if ([jsonObject isKindOfClass:[NSArray class]]) { 
    NSLog(@"its an array!"); 
    NSArray *jsonArray = (NSArray *)jsonObject; 
    NSLog(@"jsonArray - %@",jsonArray); 
    } else { 
    NSLog(@"its probably a dictionary"); 
    NSDictionary *jsonDictionary = (NSDictionary *)jsonObject; 
    NSLog(@"jsonDictionary - %@",jsonDictionary); 
} 
+0

답장을 보내 주셔서 감사합니다. IOS 개발에 상당히 익숙하다는 말을해야합니다. 데이터 및 JSON 유형의 ID를 작성하면 무슨 의미입니까? – RTB

관련 문제