Last active
April 1, 2016 01:36
-
-
Save laispace/782c970078ad791e7ce8e613be57984f to your computer and use it in GitHub Desktop.
Revisions
-
laispace revised this gist
Apr 1, 2016 . 1 changed file with 5 additions and 5 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,20 +1,20 @@ - (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); } -
laispace created this gist
Apr 1, 2016 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,26 @@ - (NSDictionary *)fetchLocalJSON:(NSString *) pathForResource { NSDictionary *dictionary; // 读取本地 JSON 文件 NSString *fileName = [[NSBundle mainBundle] pathForResource:pathForResource ofType:@"json"]; if (fileName) { NSLog(@"file exist: %@", fileName); // 读取文件内容 NSData *fileData = [[NSData alloc] initWithContentsOfFile:fileName]; // 捕获错误 NSError *error; // 转换格式 dictionary = [NSJSONSerialization JSONObjectWithData:fileData options:0 error:&error]; // 捕获错误 if (error) { NSLog(@"JSON parse error:%@", error.localizedDescription); } } else { NSLog(@"file not exist!%@", fileName); } return dictionary; }