nev*_*asy 0 json objective-c plist ios
当我调用[NSArray arrayWithContentsOfFile:]时,我得到零.
NSURL *fileURL = [NSURL URLWithString:className relativeToURL:[self JSONDataRecordsDirectory]];
return [NSArray arrayWithContentsOfFile:[NSString stringWithFormat:@"%@", [fileURL path]]];
Run Code Online (Sandbox Code Playgroud)
或者,我打电话时也会收到:
[NSArray arrayWithContentsOfURL:]
Run Code Online (Sandbox Code Playgroud)
我的所有文件的结果都在JSON中:https: //gist.github.com/neverbendeasy/03d047a6789b0ae14e1f
当我检查fileURL时,数据就在那里.
我错过了什么?
您无法使用加载JSON文件NSArray arrayWith....该方法只能加载一个以数组为根的plist文件.
如果您的文件是JSON文件,则应使用:
NSData *jsonData = [NSData dataWithContentsOfURL:fileURL];
NSError *error = nil;
NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:&error];
if (jsonArray) {
// do something with jsonArray
} else {
NSLog(@"Couldn't load JSON from %@: %@", fileURL, error);
}
Run Code Online (Sandbox Code Playgroud)
这假定JSON文件的顶级是一个数组.
| 归档时间: |
|
| 查看次数: |
957 次 |
| 最近记录: |