[英]Parsing nested objects in iOS
I'm parsing objects from my Rails app to my iOS app using JSON. 我正在使用JSON将Rails应用程序中的对象解析到iOS应用程序中。 I want to be able to show a thumbnail for each one of my posts, but the images are nested inside an array, and not only that I have many images for each post and each image has a thumb and a full image.
我希望能够为我的每个帖子显示缩略图,但是图像嵌套在数组中,不仅是每个帖子都有很多图像,而且每个图像都有一个拇指和一个完整的图像。 How can I show the thumb of the FIRST image for each of my posts?
如何为每个帖子显示FIRST图片的缩略图?
Thanks. 谢谢。
JSON 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"
}
}
}
},
{
]
}, },
My view controller 我的视图控制器
- (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;
How are you generating that JSON? 您如何生成该JSON? It's not valid, the key strings aren't quoted which is probably OK but the array elements aren't separated which I can't imagine parses at all.
这是无效的,没有引用键字符串,这可能还可以,但是数组元素没有分开,我完全无法想象解析。 I'd suggest using the JSON gem if you're not:
如果您不这样做,我建议您使用JSON gem:
require 'json'
... ...
JSON.generate({:key => "value"}) # Use your actual object of course
The structure seems a little wacky beyond that, with an image_file key nested inside an image_file key, I'd rename the outer key to something more descriptive if you have that ability. 除此之外,该结构似乎有些古怪,将image_file密钥嵌套在image_file密钥内,如果您具有此功能,则将外键重命名为更具描述性的名称。
The array should read in fine and just be a nested NSArray: 该数组应该读得很好,只是一个嵌套的NSArray:
UpcomingRelease.h : UpcomingRelease.h :
@property (nonatomic, strong) NSArray *url;
Your ViewController : 您的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"];
}
The images
entry in your upcoming_releases dictionary appears to be an array of dictionaries with 1 item in each dictionary ( image_file
) which is a dictionary. 即将发布的字典中的
images
条目似乎是一个字典数组,每个字典( image_file
)是字典,每个字典中都有一项。 However, I would note that your sample JSON for the images
entry is malformed in a couple ways: 但是,我会注意到,用于
images
条目的示例JSON格式有两种:
-there is no closing ] in the array -数组中没有关闭]
-there are no commas between the dictionary -字典之间没有逗号
-there are 3 opening brackets and 1 closing bracket -有3个打开支架和1个关闭支架
Assuming that is a typo and you fix it... 假设这是拼写错误,您可以解决它...
To store the image info in your model you would do the following: 要将图像信息存储在模型中,请执行以下操作:
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];
}
To show the thumb of the first image: 要显示第一个图像的缩略图:
NSDictionary *imageFileDictionary = [upcomingRelease.images objectAtIndex:0];
NSDictionary *imageInfoDictionary = [imageFileDictionary objectForKey@"image_file"];
NSString *thumb = [imageInfoDictionary objectForKey@"thumb"];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.