0

JSON を使用して、Rails アプリから iOS アプリへのオブジェクトを解析しています。投稿ごとにサムネイルを表示できるようにしたいのですが、画像は配列内にネストされており、投稿ごとに多くの画像があり、各画像にはサムネイルと完全な画像があるだけではありません。各投稿の FIRST 画像のサムネイルを表示するにはどうすればよいですか?

ありがとう。

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;
}

UpcomingRelease.h

@property (nonatomic, strong) NSString *url;

- (NSURL *) thumbURL;
4

2 に答える 2

0

imagescoming_releases ディクショナリのエントリは、ディクショナリである各ディクショナリ ( image_file) に 1 つの項目を持つディクショナリの配列のように見えます。ただし、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 に答える