1

ネストされたJSon配列に深く入り込もうとしています.上記のレベルでは成功しましたが、それ以上深くする方法がわかりません.

画像 @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;
}
4

2 に答える 2

2

ネストされたオブジェクトをトラバースするにはvalueForKeyPath、メソッドを使用し、ドット構文を使用して階層をドリルダウンできます。

このようなものは、辞書urlから値をフェッチします。newsItem

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

PS。接頭辞が実際に付いているプロパティがある場合は、演算子として使用される特別なトークンであるため、@使用すると問題が発生する可能性があります。この場合、代わりに次のようなことを行うことができます。valueForKeyPath@

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"];
于 2013-02-22T11:38:25.137 に答える
0

このようにディクショナリから値にアクセスしてみてください。

NSLog(@"URL: %@",[newsItem objectForKey:@"@url"]);
于 2013-02-22T11:54:10.167 に答える