0

我正在使用 JSON 将对象从我的 Rails 应用程序解析到我的 iOS 应用程序。我希望能够为我的每个帖子显示一个缩略图,但是图像嵌套在一个数组中,不仅每个帖子都有很多图像,而且每个图像都有一个拇指和一个完整的图像。如何为我的每个帖子显示第一张图片的拇指?

谢谢。

JSON

upcoming_releases: [
{
  id: 2,
  release_name: "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.jpg-2",
         thumb: {
           url: "https://s3.amazonaws.com/soleresource/uploads/releases/thumb_nike-lebron-x-low-raspberry-2.jpg"
          }
        }
      }
    },
  {
]

},

我的视图控制器

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSURL *upcomingReleaseURL = [NSURL URLWithString:@"http://obscure-lake-7450.herokuapp.com/upcoming.json"];

    NSData *jsonData = [NSData dataWithContentsOfURL:upcomingReleaseURL];

    NSError *error = nil;

    NSDictionary *dataDictionary = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error];

    self.upcomingReleases = [NSMutableArray array];

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

    for (NSDictionary *upcomingReleaseDictionary in upcomingReleasesArray) {
        UpcomingRelease *upcomingRelease = [UpcomingRelease upcomingReleaseWithName:[upcomingReleaseDictionary objectForKey:@"release_name"]];
        upcomingRelease.release_price = [upcomingReleaseDictionary objectForKey:@"release_price"];
        upcomingRelease.release_colorway = [upcomingReleaseDictionary objectForKey:@"release_colorway"];
        upcomingRelease.release_date = [upcomingReleaseDictionary objectForKey:@"release_date"];
        upcomingRelease.url = [upcomingReleaseDictionary valueForKeyPath:@"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];

    if ( [upcomingRelease.url isKindOfClass:[NSString class]]) {
        NSData *imageData = [NSData dataWithContentsOfURL:upcomingRelease.thumbURL];
        UIImage *image = [UIImage imageWithData:imageData];
        cell.imageView.image = image;
    }
    else {
//        cell.imageView.image = [UIImage imageNamed:@"cover.png"];
    }

    cell.textLabel.text = upcomingRelease.release_name;
    cell.detailTextLabel.text = [NSString stringWithFormat:@"$%@", upcomingRelease.release_price];

    return cell;
}

即将发布.h

@property (nonatomic, strong) NSString *url;

- (NSURL *) thumbURL;
4

2 回答 2

0

您的comings_releases 字典中的images条目似乎是一个字典数组,每个字典(image_file)中有一个项目,这是一个字典。但是,我会注意到您的images条目示例 JSON 格式有以下几种错误:

- 数组中没有结束 ]
- 字典之间没有逗号
- 有 3 个左括号和 1 个右括号

假设这是一个错字,你修复它......

要将图像信息存储在您的模型中,您将执行以下操作:

for (NSDictionary *upcomingReleaseDictionary in upcomingReleasesArray) {
    UpcomingRelease *upcomingRelease = [UpcomingRelease upcomingReleaseWithName:[upcomingReleaseDictionary objectForKey:@"release_name"]];
    upcomingRelease.release_price = [upcomingReleaseDictionary objectForKey:@"release_price"];
    upcomingRelease.release_colorway = [upcomingReleaseDictionary objectForKey:@"release_colorway"];
    upcomingRelease.release_date = [upcomingReleaseDictionary objectForKey:@"release_date"];
    upcomingRelease.url = [upcomingReleaseDictionary valueForKeyPath:@"url"];
    //note images property should be NSArray
    upcomingRelease.images = [upcomingReleaseDictionary objectForKey:@"images"];
    [self.upcomingReleases addObject:upcomingRelease];
}

要显示第一张图片的拇指:

NSDictionary *imageFileDictionary = [upcomingRelease.images objectAtIndex:0];
NSDictionary *imageInfoDictionary = [imageFileDictionary objectForKey@"image_file"];
NSString *thumb = [imageInfoDictionary objectForKey@"thumb"];
于 2013-09-01T20:46:34.443 回答
0

你是如何生成那个 JSON 的?这是无效的,键字符串没有被引用,这可能没问题,但是数组元素没有被分隔,我根本无法想象解析。如果您不是,我建议您使用 JSON gem:

require 'json'

...

JSON.generate({:key => "value"}) # Use your actual object of course

除此之外,结构似乎有点古怪,一个 image_file 键嵌套在一个 image_file 键中,如果你有这种能力,我会将外部键重命名为更具描述性的东西。

该数组应该可以很好地读取,并且只是一个嵌套的 NSArray:

即将发布.h

@property (nonatomic, strong) NSArray *url;

你的视图控制器

upcomingRelease.release_date = [upcomingReleaseDictionary objectForKey:@"release_date"];
upcomingRelease.url = [upcomingReleaseDictionary valueForKeyPath:@"url"];
upcomingRelease.images = [upcomingReleaseDictionary objectForKey:@"images"];

[self.upcomingReleases addObject:upcomingRelease];

...

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];

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

if ([upcomingRelease.images isKindOfClass:[NSArray class]] && [upcomingRelease.images count])
    thumbURLString = [[[[[upcomingRelease.images objectAtIndex:0] objectForKey:@"image_file"] objectForKey:@"image_file"] objectForKey:@"thumb"] objectForKey:@"url"];

if (thumbURL)
{
    NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:thumbURLString]];
    UIImage *image = [UIImage imageWithData:imageData];
    cell.imageView.image = image;
}
else {
//  cell.imageView.image = [UIImage imageNamed:@"cover.png"];
}
于 2013-09-01T20:43:41.463 回答