Objective c 解析嵌套JSON对象

Objective c 解析嵌套JSON对象,objective-c,json,Objective C,Json,现在我正在学习如何使用JSON从web获取数据。我想为我的每一篇文章(从Rails应用程序)获取一张图片。根据Treehouse Library教程,我必须执行以下操作: TableViewController.m self.upcomingReleases = [NSMutableArray array]; NSArray *upcomingReleasesArray = [dataDictionary objectForKey:@"upcoming_releases"]; for (NS

现在我正在学习如何使用JSON从web获取数据。我想为我的每一篇文章(从Rails应用程序)获取一张图片。根据Treehouse Library教程,我必须执行以下操作:

TableViewController.m

self.upcomingReleases = [NSMutableArray array];

NSArray *upcomingReleasesArray = [dataDictionary objectForKey:@"upcoming_releases"];

for (NSDictionary *upcomingReleaseDictionary in upcomingReleasesArray) {
    UpcomingRelease *upcomingRelease = [UpcomingRelease upcomingReleaseWithReleaseName:[upcomingReleaseDictionary objectForKey:@"release_name"]];
    upcomingRelease.thumbnail = [upcomingReleaseDictionary objectForKey:@"thumbnail"];
    upcomingRelease.url = [NSURL URLWithString:[upcomingReleaseDictionary objectForKey:@"url"]];
    [self.upcomingReleases addObject:upcomingRelease];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    UpcomingRelease *upcomingRelease = [self.upcomingReleases objectAtIndex:indexPath.row];

    NSData *imageData = [NSData dataWithContentsOfURL:upcomingRelease.thumbnailURL];
    UIImage *image = [UIImage imageWithData:imageData];

    cell.imageView.image = image;
    cell.textLabel.text = upcomingRelease.release_name;

    return cell;
}
- (NSURL *) thumbnailURL {
    return [NSURL URLWithString:self.thumbnail];
}
UpcomingRelease.h

@property (nonatomic, strong) NSString *release_name;
@property (nonatomic, strong) NSString *thumbnail;
@property (nonatomic, strong) NSURL *url;

//Designated Initializer
- (id) initWithReleaseName:(NSString *)release_name;
+ (id) upcomingReleaseWithReleaseName:(NSString *)release_name;

- (NSURL *) thumbnailURL;
- (NSString *) formattedDate;

@end
UpcomingRelease.m

self.upcomingReleases = [NSMutableArray array];

NSArray *upcomingReleasesArray = [dataDictionary objectForKey:@"upcoming_releases"];

for (NSDictionary *upcomingReleaseDictionary in upcomingReleasesArray) {
    UpcomingRelease *upcomingRelease = [UpcomingRelease upcomingReleaseWithReleaseName:[upcomingReleaseDictionary objectForKey:@"release_name"]];
    upcomingRelease.thumbnail = [upcomingReleaseDictionary objectForKey:@"thumbnail"];
    upcomingRelease.url = [NSURL URLWithString:[upcomingReleaseDictionary objectForKey:@"url"]];
    [self.upcomingReleases addObject:upcomingRelease];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

    UpcomingRelease *upcomingRelease = [self.upcomingReleases objectAtIndex:indexPath.row];

    NSData *imageData = [NSData dataWithContentsOfURL:upcomingRelease.thumbnailURL];
    UIImage *image = [UIImage imageWithData:imageData];

    cell.imageView.image = image;
    cell.textLabel.text = upcomingRelease.release_name;

    return cell;
}
- (NSURL *) thumbnailURL {
    return [NSURL URLWithString:self.thumbnail];
}
我应该用我自己的字符串替换所有的“缩略图”。问题是,我的图像位于嵌套对象中,并且有多个。我想从每个帖子中的第一个图像文件调用拇指的URL。我该怎么做

谢谢

我的JSON API

upcoming_releases: [
{
  id: 2,
  release_name: "Nike Lebron X Low",
  release_price: "165",
  release_colorway: "Raspberry-Red/Blueprint-Court",
  release_date: "2013-09-07T00:00:00.000Z",
  url: "http://obscure-lake-7450.herokuapp.com/upcoming/2",
  images: [
    {
      image_file: {
        image_file: {
          url: "https://s3.amazonaws.com/soleresource/uploads/releases/nike-lebron-x-low-raspberry.jpg",
          thumb: {
            url: "https://s3.amazonaws.com/soleresource/uploads/releases/thumb_nike-lebron-x-low-raspberry.jpg"
          }
        }
      }
    },
    {
      image_file: {
        image_file: {
          url: "https://s3.amazonaws.com/soleresource/uploads/releases/nike-lebron-x-low-raspberry-2.jpg",
          thumb: {
            url: "https://s3.amazonaws.com/soleresource/uploads/releases/thumb_nike-lebron-x-low-raspberry-2.jpg"
          }
        }
      }
    },
  ]
}

假设您可以依赖JSON进给的结构,将其转换为基础对象(在本例中<代码> NSArray <代码>):

您可以使用来帮助解析数据。例如,要从URL获取数据,然后将其解析到
NSDictionary
,可以执行以下操作:

NSData *jsonData = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://www.example.com"]];
SBJsonParser *parser = [[SBJsonParser alloc] init];
NSDictionary *response = [parser objectWithData:jsonData];

关于如何实现这一点,请查看此APP.NET For iOS。显示迄今为止解析JSON和提取所需数据的代码。我刚刚更新了我的问题。请访问JSON.org并学习JSON语法。特别要注意的是被称为“数组”的东西。