-1

现在我正在学习如何使用 JSON 从网络获取数据。我想为我的每篇文章获取一张图片(来自 Rails 应用程序)。按照树屋图书馆教程,我将不得不做这样的事情:

表视图控制器.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;
}

即将发布.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

即将发布.m

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

我应该用我自己的字符串替换所有“缩略图”。问题是我的图像在一个嵌套对象内,并且不止 1 个。我想从我的每个帖子中的第一个 image_file 调用拇指的 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"
          }
        }
      }
    },
  ]
}
4

2 回答 2

0

假设您可以依赖 JSON 提要的结构,将其转换为基础对象(NSArray在本例中):

NSString *fileURL;
NSString *thumbURL;
if (jsonArray.count) {
   NSDictionary *firstRelease = jsonArray[0];
   NSArray *images = firstRelease[@"images"];
   if (images.count) {
      NSDictionary *firstImage = images[0][@"image_file"];
      fileURL = firstImage[@"image_file"];
      thumbURL = firstImage[@"thumb"];
   }
}
于 2013-08-22T17:39:45.690 回答
0

您可以使用SBJSON来帮助解析数据。例如,要从 URL 获取数据,然后将其解析为NSDictionary,您可以这样做:

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

有关如何执行此操作的良好工作示例,请查看此适用于 iOS 的APP.NET公共流客户端。

于 2013-08-22T20:26:50.767 回答