En ce moment j'apprends comment obtenir des données du Web en utilisant JSON. Je veux obtenir une image pour chacun de mes messages (à partir d'une application Rails). Suite à la bibliothèque Treehouse tutoriel, je dois faire quelque chose comme ceci:Analyse des objets JSON imbriqués
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];
}
Je suis censé remplacer toute la "vignette" avec ma propre chaîne. Le problème est que mes images sont à l'intérieur d'un objet imbriqué et il y en a plus de 1. Je veux appeler l'URL du pouce du premier fichier image_file dans chacun de mes messages. Comment puis-je faire ceci?
Merci.
Mon 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"
}
}
}
},
]
}
Afficher le code que vous avez jusqu'à présent pour analyser le JSON et extraire les données dont vous avez besoin. – rmaddy
Je viens de mettre à jour ma question. – ChrisBedoya
Veuillez vous rendre sur json.org et étudier la syntaxe JSON. En particulier noter la chose appelée un "tableau". –