Skip to content

Instantly share code, notes, and snippets.

@paulkaplan
Created November 22, 2011 19:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paulkaplan/1386683 to your computer and use it in GitHub Desktop.
Save paulkaplan/1386683 to your computer and use it in GitHub Desktop.
Requesting and parsing json data from rails app in objective-c
+(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