2013-02-04 36 views
1

我一直在尋找通過堆棧和我不能得到嵌套的JSON工作,嵌套的Json的Xcode 4.5

我不斷收到這個錯誤,我的數據不會被裝載到表視圖。我的代碼工作,如果我不使用嵌套。

2013年2月4日13:17:20.961大浪應用[2338:C07] - [__ NSCFDictionary objectAtIndex:]:無法識別的選擇發送到實例0x7165510

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view from its nib. 

    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES; 

    NSURL *url = [NSURL URLWithString:@"http://blog.bigwavemedia.co.uk/?json=get_recent_posts"]; 
    NSURLRequest *request = [NSURLRequest requestWithURL:url]; 
    [[NSURLConnection alloc] initWithRequest:request delegate:self]; 

} 

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{ 
    data = [[NSMutableData alloc] init]; 


} 

-(void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)theData{ 
    [data appendData:theData]; 
} 


-(void) connectionDidFinishLoading:(NSURLConnection *)connection{ 
    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO; 

    news = [NSJSONSerialization JSONObjectWithData:data options:nil error:nil]; 
    [mainTableView reloadData]; 

} 

-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{ 
    UIAlertView *errorView = [[UIAlertView alloc] initWithTitle:@"error" message:@"the download could not complete" delegate:nil cancelButtonTitle:@"Dismiss" otherButtonTitles:nil]; 
    [errorView show]; 

    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO; 

} 
-(int)numberOfSectionsInTableView:(UITableView *)tableView{ 
    return 1; 
} 

-(int)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ 
    return [news count]; 
} 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MainCell"]; 
    if (cell == nil) { 
     cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"MainCell"]; 
    } 

    cell.textLabel.text = [[[news objectAtIndex:indexPath.row] objectForKey:@"title"] valueForKey:@"id"]; 

    return cell; 
} 

任何幫助將是巨大的,這是我的第一個xcode項目,對我來說都是新的。

http://jsonviewer.stack.hu/#http://blog.bigwavemedia.co.uk/?json=get_recent_postshttp://blog.bigwavemedia.co.uk/?json=get_recent_posts

+0

你試圖使用一個對象作爲一個數組,而它是一個字典。檢查你的邏輯和JSON。 – 2013-02-04 13:29:40

+0

你能幫我嗎?我需要改變什麼? – Brent

+0

您希望在tableview中顯示的響應的值是多少? – Dilip

回答

1

添加sbjson庫在project.and嘗試使用此代碼,而不是你的事。 在.h文件中

NSMUtableArray * idArray; 

和.m文件

 #import "SBJson.h" 

- (void)viewDidLoad 
{  

idArray = [[NSMutableArray alloc] init]; 

NSString *post [email protected]""; 

       NSURL *url=[NSURL URLWithString:@"http://blog.bigwavemedia.co.uk/?json=get_recent_posts"]; 

       NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]; 

       NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]]; 

       NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init]; 
       [request setURL:url]; 
       [request setHTTPMethod:@"POST"]; 
       [request setValue:postLength forHTTPHeaderField:@"Content-Length"]; 
       //[request setValue:@"application/json" forHTTPHeaderField:@"Accept"]; 
       // [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; 
       [request setHTTPBody:postData]; 

       //[NSURLRequest setAllowsAnyHTTPSCertificate:YES forHost:[url host]]; 

       NSError *error = [[NSError alloc] init]; 
       NSHTTPURLResponse *response = nil; 
       NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]; 



       //NSLog(@"Response code: %d", [response statusCode]); 
       if ([response statusCode] >=200 && [response statusCode] <300) 
       { 
        NSString *responseData = [[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding]; 
        // NSLog(@"Response ==> %@", responseData);      

        SBJsonParser *jsonParser = [SBJsonParser new]; 
        NSDictionary *jsonData = (NSDictionary *) [jsonParser objectWithString:responseData error:nil]; 


        //NSLog(@"json data is : %@",jsonData); 

        NSArray *allDataArray = [[NSArray alloc]init]; 
        allDataArray= [jsonData objectForKey:@"posts"]; 


        for(int i=0;i<[allDataArray count];i++) 
        { 
         NSDictionary *tempDictionary = [allDataArray objectAtIndex:i]; 


         if([tempDictionary objectForKey:@"id"]!=nil) 
         { 

          [idArray addObject:[tempDictionary objectForKey:@"id"]]; 
         } 

        } 

       } else { 
        if (error) NSLog(@"Error: %@", error); 
        [self alertStatus:@"Connection Failed" :@"Does not find any data!"]; 
       } 


      } 
      @catch (NSException * e) { 
       NSLog(@"Exception: %@", e); 
       [self alertStatus:@"Data not found." :@"Does not find any data!"]; 
      } 
[super viewDidLoad]; 
} 

UPDATE

而對於tableview中添加此

-(int)numberOfSectionsInTableView:(UITableView *)tableView{ 
    return 1; 
} 

-(int)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ 
    return [news count]; 
} 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MainCell"]; 
    if (cell == nil) { 
     cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"MainCell"]; 
    } 

    cell.textLabel.text = [idArray objectAtIndex:indexPath.row]; 

    return cell; 
} 
+0

非常感謝答覆,所以我將這一切都添加到viewDidLoad部分或我的m? – Brent

+0

viewdidload會很好。 – Dilip

+0

並且不要忘記添加SBJson庫 – Dilip