现在我正在学习如何使用JSON从网上获取数据。我想为每个帖子(来自Rails应用程序)获取图像。按照树屋图书馆教程,我必须做这样的事情:
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"
}
}
}
},
]
}
答案 0 :(得分: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"];
}
}
答案 1 :(得分: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 public stream client。