Skip to content

Instantly share code, notes, and snippets.

@laispace
Last active April 1, 2016 01:36
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 laispace/782c970078ad791e7ce8e613be57984f to your computer and use it in GitHub Desktop.
Save laispace/782c970078ad791e7ce8e613be57984f to your computer and use it in GitHub Desktop.
fetch local json file and parse it to a dictionary
- (NSDictionary *)fetchLocalJSON:(NSString *) pathForResource {
NSDictionary *dictionary;
// read JSON file
NSString *fileName = [[NSBundle mainBundle] pathForResource:pathForResource ofType:@"json"];
if (fileName) {
NSLog(@"file exist: %@", fileName);
// get file data
NSData *fileData = [[NSData alloc] initWithContentsOfFile:fileName];
// catch error
NSError *error;
// parse to dictionary
dictionary = [NSJSONSerialization JSONObjectWithData:fileData options:0 error:&error];
// error
if (error) {
NSLog(@"JSON parse error:%@", error.localizedDescription);
}
} else {
NSLog(@"file not exist!%@", fileName);
}
return dictionary;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment