Created
November 22, 2011 19:39
-
-
Save paulkaplan/1386682 to your computer and use it in GitHub Desktop.
Requesting and parsing json data from rails app in objective-c
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
+(NSMutableArray *) loadRemoteListings { | |
NSMutableArray *listings = [NSMutableArray arrayWithCapacity:20]; | |
NSURL *url = [[NSURL alloc] initWithString:@"http://localhost:3000"]; | |
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; | |
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"]; | |
NSURLResponse *response; | |
NSError *error; | |
NSData *response_data = [NSURLConnection sendSynchronousRequest:request | |
returningResponse:&response | |
error:&error]; | |
NSDictionary* json = [NSJSONSerialization | |
JSONObjectWithData:response_data | |
options:kNilOptions | |
error:&error]; | |
for ( NSDictionary *list in json ) { | |
Listing *listing = [[Listing alloc] init]; | |
listing.details = [list valueForKey:@"details"]; | |
listing.description= [list valueForKey:@"description"]; | |
[listings addObject:listing]; | |
} | |
return listings; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment