使用Objective-C访问另一个顶级JSON对象

时间:2013-04-22 19:06:17

标签: objective-c json

我很难在我的应用程序中访问另一个顶级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;
        }

显然计数部分不起作用(它只返回三个)。但是,如何计算“数据”对象中的所有对象? 所有帮助都非常感谢!我已经完成了我的谷歌搜索,但我似乎无法找到我看起来的确切内容。

0 个答案:

没有答案