Last active
April 1, 2016 01:36
-
-
Save laispace/782c970078ad791e7ce8e613be57984f to your computer and use it in GitHub Desktop.
fetch local json file and parse it to a dictionary
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
- (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