Skip to content

Instantly share code, notes, and snippets.

@kristopherjohnson
Created October 25, 2012 11:36
Show Gist options
  • Save kristopherjohnson/3952120 to your computer and use it in GitHub Desktop.
Save kristopherjohnson/3952120 to your computer and use it in GitHub Desktop.
Read JSON resource into an NSDictionary
- (NSDictionary *)readJSONResourceNamed:(NSString *)resourceName {
NSString *filePath = [[NSBundle mainBundle] pathForResource:resourceName
ofType:@"json"];
NSData *fileData = [NSData dataWithContentsOfFile:filePath];
if (!fileData)
return nil;
NSError *error = nil;
id jsonObject = [NSJSONSerialization JSONObjectWithData:fileData options:0 error:&error];
if (!jsonObject || error)
return nil;
if ([jsonObject isKindOfClass:[NSDictionary class]])
return (NSDictionary *)jsonObject;
else
return nil;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment