2014-03-24 96 views
0

這是給定的API,我必須解析。我還必須在我的UITableView上顯示名稱。我想獲取名稱的值n在表格視圖中顯示它。 這是代碼:JSON解析:在UITableView上顯示數據

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{ 

    return [ episodes count]; 
} 
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *simpleTableIdentifier = @"SimpleTableItem"; 

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier]; 

    if (cell == nil) { 
     cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier]; 
    } 

    cell.textLabel.text = [[ episodes objectAtIndex:indexPath.row] objectForKey:@"names"]; 


    return cell; 
} 
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath 
{ 

    UIAlertView *logoutConfirm = [[UIAlertView alloc]initWithTitle:@"Toilet Finder" message:@"" delegate:self cancelButtonTitle:nil otherButtonTitles:@"Display on Map",@"Route",@"Cancel", nil]; 
    logoutConfirm.tag = 111; 
    [logoutConfirm show]; 

} 

- (void) parseJSONWithURL:(NSURL *) jsonURL 

{ 
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0); 
    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES; 
dispatch_async(queue, ^{ 
     NSError *error = nil; 
    NSString *json = [NSString stringWithContentsOfURL:jsonURL 
               encoding:NSASCIIStringEncoding error:&error]; 
    if (error == nil){ 
     [email protected]"http://api.kivaws.org/v1/loans/search.json?status=fundraising"; 
     NSLog(@"URL %@", WebInServe); 
     NSURL *Final_Url = [NSURL URLWithString:WebInServe]; 

     NSData* jsonData = [NSData dataWithContentsOfURL: Final_Url]; 


     NSError* error; 

     jsonDict = [NSJSONSerialization JSONObjectWithData:jsonData options:kNilOptions error:&error]; 
     NSLog(@"What is contain let me check %@",jsonDict); 
     NSArray* latestLoans = [jsonDict objectForKey:@"loans"]; 

     NSLog(@"loans in array: %@", latestLoans); 


      episodes = [[jsonDict valueForKey:@"latestLoans"]valueForKey:@"name"]; 


       if (error == nil) 
     { 
      dispatch_async(dispatch_get_main_queue(), ^{ 
       [UIApplication sharedApplication].networkActivityIndicatorVisible = NO; 
        episodes = [[jsonDict valueForKey:@"latestLoans"]valueForKey:@"name"]; 
       [tableView reloadData]; 
      }); 
     } 

我已經嘗試使用:

episodes = [[jsonDict valueForKey:@"latestLoans"]objectForKey:@"name"]; 

我已經使用幾個tymes objectForKeyvalueForkey但在上面的代碼,並在

cell.textLabel.text = [[ episodes objectAtIndex:indexPath.row] objectForKey:@"names"]; 

線的代碼cellForRowAtIndexPath: method

但數據不會顯示在tableview中......事實上,這是顯示在NSLog ...

+1

檢查本教程:http://www.raywenderlich.com/5492/working-with-json-in-ios-5 –

回答

0

爲什麼要使用dispatch_queues而不是使用NSURLConnection的專用異步API?

正如Apple的「併發編程指南」中所述,當已有專用API以併發方式執行某些任務時,請避免通過在異步隊列中包裝同步調用(如+[NSString stringWithContentOfURL:]) (如在你的情況下的併發隊列上的dispatch_async)。


此外,您latestLoans變量(你再NSLog)提取從一個叫@"loans"關鍵的內容,但在當你嘗試提取集的下一行,你從字典中提取它假定關聯到@"latestLoans"鍵。你的代碼可能存在一些差異,不是嗎? 這可能是你的代碼中的主要問題。


最後有時候你使用valueForKey:(這是用內省的作品,一點都沒有專門的NSDictionary特異性方法的通用鍵 - 值編碼方法),而不是objectForKey:(這是你需要什麼在訪問NSDictionary中的對象時使用)。

當然valueForKey:會工作,但objectForKey:NSDictionary的專用方法,它會更有效率。 個人而言,我甚至更喜歡新的「Objective-C的現代符號」爲對象常量,即dict[key],編譯器會自動轉化爲objectForKey:你(這[]符號僅僅是一些syntaxic糖更簡潔)

所以在這裏說到我的代碼的建議:

... 
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse* resp, NSData* jsonData, NSError* err) { 
    ... 
    NSDictionary* jsonDict = [NSJSONSerialization JSONObjectWithData:jsonData options:kNilOptions error:&error]; 
    NSArray* latestLoans = jsonDict[@"loans"]; 
    self.episodes = latestLoans[@"name"]; 
    [self.tableView reloadData]; 
}]; 
0

看來問題就在這裏:

集= [[jsonDict valueForKey:@ 「latestLoans」] valueForKey:@ 「名」];

JSON響應的根目錄只有兩個項目 - 「分頁」和「貸款」。你正在要求「latestLoans」。由於JSON的根級別中沒有這樣的項目,因此您將在劇集中獲得零。 我認爲你應該使用你的日誌進行呼叫:

的NSArray * latestLoans = [jsonDict objectForKey:@ 「貸款」];

嘗試用

發作= [jsonDict objectForKey:@ 「貸款」];

代替

發作= [[jsonDict valueForKey:@ 「latestLoans」] valueForKey:@ 「名稱」];