2013-08-22 217 views
-1

现在我正在学习如何使用JSON从Web获取数据。我想为我的每篇文章(从Rails应用程序)获取图片。继树库教程中,我会做这样的事情:解析嵌套的JSON对象

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

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

- (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" 
      } 
     } 
     } 
    }, 
    ] 
} 
+2

显示你的代码迄今解析JSON和提取你需要的数据。 – rmaddy

+0

我刚刚更新了我的问题。 – ChrisBedoya

+1

请转到json.org并研究JSON语法。特别要注意的是所谓的“阵列”。 –

回答

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"]; 
    } 
} 
0

你可以使用SBJSON来帮助解析数据。例如,为了从URL中的数据,然后将其解析为一个NSDictionary,你可以这样做:

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

有关如何做到这一点的好,工作实例,看看这个APP.NET public stream client为iOS 。