-2

我正在尝试解析包含图像链接的 json 文件。这是我的代码:

#import "Pictures.h"
#import "DEMONavigationController.h"
#import "PicturesObject.h"

@interface Pictures ()
{
    NSInteger refreshIndex;
    NSArray *images;
    NSMutableArray *jsonIs;
    NSArray *items;
    IBOutlet UIImageView *imagesinsta;
}
@end

@implementation Pictures

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title = @"Pictures";
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Menu"
                                                                             style:UIBarButtonItemStylePlain
                                                                            target:(DEMONavigationController *)self.navigationController
                                                                            action:@selector(showMenu)];

    // ***************** FETCHING DATA  ******************* //
    NSURL *URL = [NSURL URLWithString:[NSString stringWithFormat:@"link-to-json.php?image=Image"]];
    NSData *data= [NSData dataWithContentsOfURL:URL];
    if (data == nil) {
        return;
    }
    NSError* error;
    items = [NSJSONSerialization JSONObjectWithData:data
                                            options:kNilOptions
                                              error:&error];

    NSLog(@"Json : %@",jsonIs);

    if (jsonIs != nil) {
        NSMutableDictionary* aDict = jsonIs[0];
        NSString *item_media = [aDict objectForKey:@"link"];
    }

    // ***************** FETCHING DATA  ******************* //
}

#pragma mark - Table view data source

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *simpleTableIdentifier = @"PicturesObject";

    PicturesObject *cell = (PicturesObject *)[tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
    if (cell == nil)
    {
        NSArray *nib = [[NSBundle mainBundle] loadNibNamed:@"PicturesObject" owner:self options:nil];
        cell = [nib objectAtIndex:0];
    }

    // The element in the array is going to be a dictionary. I JUST KNOW THIS. The key for the tweet is "text".
    NSDictionary *item = [items objectAtIndex:indexPath.row];
    NSString *item_media = [item objectForKey:@"link"];

    return cell;
}

- (void)issueLoadRequest
{
    // Dispatch this block asynchronosly. The block gets JSON data from the specified URL and performs the proper selector when done.
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        NSData* data = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"link-to-json.php?image=Image"]];
        [self performSelectorOnMainThread:@selector(receiveData:) withObject:data waitUntilDone:YES];
    });
}

- (void)receiveData:(NSData *)data {
    // When we have the data, we serialize it into native cocoa objects. (The outermost element from twitter is
    // going to be an array. I JUST KNOW THIS. Reload the tableview once we have the data.
    self.tweets = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
    [self.myTableView reloadData];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.tweets.count;
}

@end

这是我的自定义 PicturesObject 表视图的样子:

图片对象

但是当我启动我的应用程序时,我只会看到这样的屏幕:

表视图

4

1 回答 1

0

Json Data 类似于 Dictionary 而不是数组。而不是使用

NSArray items

利用

NSDictionary items

然后从字典中提取相关数据。

于 2013-11-09T13:23:40.033 回答