Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
tat
447 PointsCreate a Method ?
I wan to parse JSON from 2 URLs from youtube and load all the results on one UITableView.
Do i have to use JSON Serialization twice or is it possible to create a method ?
How do i create a Method? Here my code for getting JSON from one URL:
- (void)viewDidLoad
{
[super viewDidLoad];
NSString *urlAsString = @"http://gdata.youtube.com/feeds/api/playlists/PL0l3xlkh7UnvLdr0Zz3XZZuP2tENy_qaP?v=2&alt=jsonc&max-results=50";
NSURL *url = [NSURL URLWithString:urlAsString];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
// This will initiate the request and parse the data using apples JSONSerialization
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
self.videoMetaData = [JSON valueForKeyPath:@"data.items.video"];
// This will have all the sq and hq thumbnails
self.allThumbnails = [JSON valueForKeyPath:@"data.items.video.thumbnail"];
// Grab the video ID
self.videoID = [JSON valueForKeyPath:@"data.items.video.videoID"];
[self.tableView reloadData]; // Must Reload
// NSLog(@" video Meta Data %@", self.videoMetaData);
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
NSLog(@"Request Failed with Error: %@, %@", error, error.userInfo);
}];
[operation start];
2 Answers
tat
447 PointsI tried this but i could not make it work:
-(void)getJSONfromURL:(NSURL *)url {
NSURLRequest *request = [NSURLRequest requestWithURL:url];
// setup AFNetworking stuff
AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
// call delegate or processing method on success
myJSON = (NSMutableArray *)JSON;
} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
NSLog(@"Request Failed with Error: %@, %@", error, error.userInfo);
}];
[operation start];
}
Amit Bijlani
Treehouse Guest Teacher@tauseef that should work except that you probably want to append to your mutable array instead of writing over it.
[myJSON addObjectsFromArray:(NSArray *)JSON];