Skip to content

Instantly share code, notes, and snippets.

@laispace
Last active April 1, 2016 01:36
Show Gist options
  • Save laispace/782c970078ad791e7ce8e613be57984f to your computer and use it in GitHub Desktop.
Save laispace/782c970078ad791e7ce8e613be57984f to your computer and use it in GitHub Desktop.

Revisions

  1. laispace revised this gist Apr 1, 2016. 1 changed file with 5 additions and 5 deletions.
    10 changes: 5 additions & 5 deletions fetchLocalJSON.m
    Original file line number Diff line number Diff line change
    @@ -1,20 +1,20 @@
    - (NSDictionary *)fetchLocalJSON:(NSString *) pathForResource {
    NSDictionary *dictionary;
    // 读取本地 JSON 文件
    // 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);
    }
  2. laispace created this gist Apr 1, 2016.
    26 changes: 26 additions & 0 deletions fetchLocalJSON.m
    Original 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;
    }