2013-02-22 94 views
1

我試圖深入到一個嵌套的JSon數組,我已經成功地完成了上面的級別,但我不能解決如何獲得更深入的。NSJSONSerialization Json嵌套Sub

我需要登錄圖片@url我附上了一個屏幕顯示,以顯示我需要它去的地方。

謝謝:)

json

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 


    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES; 

    NSURL *url = [NSURL URLWithString:@"http://api.storageroomapp.com/accounts/511a4f810f66026b640007b8/collections/511a51580f66023bff000ce9/entries.json?auth_token=Zty6nKsFyqpy7Yp5DP1L&preview_api=1"]; 

    NSURLRequest *request = [NSURLRequest requestWithURL:url]; 
    [[NSURLConnection alloc] initWithRequest:request delegate:self]; 


} 

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

    NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data options:nil error:nil]; 

    NSDictionary *arrayDictionary = dictionary[@"array"]; 

    news = arrayDictionary[@"resources"]; 

    [tableView reloadData]; 
} 





-(void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{ 
    UIAlertView *errorView = [[UIAlertView alloc] initWithTitle:@"Error" message:@"The data could not be downloaded - please make sure you're connected to either 3G or Wi-FI" 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{ 

    NSDictionary *newsItem = news[[indexPath row]]; 


    NSString *title = newsItem[@"title"]; 
    NSString *date = newsItem[@"date"]; 
    NSString *thumb = newsItem[@"tablethumb"]; 




    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MainCell"]; 

    if (cell == nil) { 
     cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"MainCell"]; 

     [[cell textLabel] setText:title]; 
     [[cell detailTextLabel] setText:date]; 





     if((NSNull *)thumb == [NSNull null]){ 
      NSLog(@"no image"); 
     } else{ 
      NSLog(@ "image = %@", thumb); 

     } 


    } 
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; 
    return cell; 
} 

回答

2

遍歷嵌套的對象,你可以使用valueForKeyPath方法和使用點語法鑽到hieararchy。

像這樣的東西會從你的字典newsItemurl值:

NSDictionary *newsItem = news[[indexPath row]]; 
NSString *thumbUrl = [newsItem valueForKeyPath:@"tablethumb.url"]; 

PS。如果您確實擁有以@爲前綴的屬性,那麼您可能會因使用valueForKeyPath而陷入困境,因爲@is a special token used as an operator。在這種情況下,你可以做這樣的事情,而不是:

NSDictionary *newsItem = news[[indexPath row]]; 
id tablethumb = [newsItem objectForKey:@"tablethumb"]; 
NSString *thumbUrl = @""; 
// Check if not null and access the @url 
if (tablethumb != [NSNull null]) 
    thumbUrl = tablethumb[@"@url"]; 
+0

感謝您的快速回應,我試過上面的代碼和我的控制檯說。 image =(null),然後我嘗試@「tablethumb。@ url」,並在NSUnkownKeyException的最後一個日誌中出現錯誤。 – Brent 2013-02-22 11:44:17

+0

是的,只是意識到你有實際的'@'作爲前綴。請參閱我的編輯答案 – Alladinian 2013-02-22 11:48:04

+0

@BrentFrench另請注意,在您的代碼中,您將'tablethumb'視爲字符串,而在您的json中將其視爲字典... – Alladinian 2013-02-22 11:51:20

0

嘗試訪問值從字典這樣,

NSLog(@"URL: %@",[newsItem objectForKey:@"@url"]); 
+0

這不會工作,因爲那裏已經有一個頂級的網址。不管怎麼說,還是要謝謝你 – Brent 2013-02-22 11:59:19