我很难在我的应用程序中访问另一个顶级 JSON 对象。该计划是从 Instagram 加载数据(有效),但不计算我想要的顶级数据。
JSON 文档可以在这里找到 ( http://pastebin.com/0Z4vBjRn ) 基本上它有三个顶级对象:分页、元和数据(我想要数据位)。
这是我现在的代码:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
// Viser "spinner" for å symbolisere nettverkstrafikk.
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
// URL til JSON filen
NSURL *newsUrl = [NSURL URLWithString:@"https://api.instagram.com/v1/tags/beer/media/recent?client_id=client_key"];
//URL Requestobjekt for å kontrollere tilkobling
NSURLRequest *newsRequest = [NSURLRequest requestWithURL:newsUrl];
[[NSURLConnection alloc]initWithRequest:newsRequest delegate:self];
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
[rawImages setLength:0];
rawImages = [[NSMutableData alloc] init];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
[rawImages appendData:data];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
images = [[NSArray alloc]init]; // Updated
images = [NSJSONSerialization JSONObjectWithData:rawImages options:0 error:0];
[self.collectionView reloadData];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
UIAlertView *errorView = [[UIAlertView alloc]initWithTitle:@"Feil" message:@"Det har oppstått en feil ved nedlastingen av data. Dobbeltsjekk at du er koblet til internett" delegate:nil cancelButtonTitle:@"Fortsett" otherButtonTitles:nil];
[errorView show];
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return [images count];
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
UICollectionViewCelle *cell = (UICollectionViewCelle *)[collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
cell.imageView.image = [UIImage imageNamed:@"eksempel.jpg"];
return cell;
}
显然计数部分不起作用(它只返回三个)。但是我怎样才能让它计算“数据”对象中的所有对象呢?高度赞赏所有帮助!我已经完成了我的谷歌搜索,但我似乎无法准确找到我在寻找什么。