Skip to content

Instantly share code, notes, and snippets.

@teofiloisrael
Last active December 16, 2015 05:38
Show Gist options
  • Save teofiloisrael/5385326 to your computer and use it in GitHub Desktop.
Save teofiloisrael/5385326 to your computer and use it in GitHub Desktop.
Parseando JSON desde un API con Objective-C
#import "ViewController.h"
#import "JSONKit.h"
@interface ViewController ()
@property (nonatomic, retain) NSMutableData *response;
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.response = [NSMutableData data];
NSURL *url = [NSURL URLWithString:@"http://www.emplea.do/jobs.json"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[[NSURLConnection alloc] initWithRequest:request delegate:self];
}
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
[self.response setLength:0];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
[self.response appendData:data];
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
UIAlertView *a = [[UIAlertView alloc] initWithTitle:@"DevelopersDO" message:@"Houston, tenemos un problema" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
[a release];
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection{
//Aquí se parsea el JSON
JSONDecoder *jsonDecoder = [[JSONDecoder alloc] init];
NSError *err;
NSDictionary *results = [jsonDecoder objectWithData:self.response error:&err];
if (err) {
NSLog(@"%@", [err description]);
}else{
NSLog(@"%@",[results description]);
}
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment