我正在使用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;
}
UpcomingRelease.h
@property (nonatomic, strong) NSString *url;
- (NSURL *) thumbURL;
答案 0 :(得分:0)
你是如何生成那个JSON的?这是无效的,关键字符串没有被引用,这可能是好的,但数组元素没有分开,我无法想象解析。如果你不是,我建议使用JSON gem:
require 'json'
...
JSON.generate({:key => "value"}) # Use your actual object of course
结构看起来有点古怪,有一个image_file键嵌套在image_file键中,如果你有这种能力,我会把外键重命名为更具描述性的东西。
数组应该读得很好,只是一个嵌套的NSArray:
<强> UpcomingRelease.h 强>:
@property (nonatomic, strong) NSArray *url;
您的ViewController :
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"];
}
答案 1 :(得分:0)
您的coming_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"];