use*_*278 13 iphone objective-c nsdata ios
我正在尝试使用此代码获取JSON存在于我的xcode资源中的文件
-(void)readJsonFiles
{
NSString *str=[[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"classes.json"];
NSLog(@"Path: %@", str);
NSData *fileData = [NSData dataWithContentsOfFile:str];
NSLog(@"Data: %@", fileData);
SBJsonParser *parser = [[SBJsonParser alloc] init] ;
NSDictionary *jsonObject = [parser objectWithData:fileData];
NSLog(@"%@", jsonObject);
}
Run Code Online (Sandbox Code Playgroud)
path返回文件路径的这个链接
/Users/astutesol/Library/Application Support/iPhone Simulator/6.0/Applications/A4E1145C-500C-4570-AF31-9E614FDEADE4/The Gym Factory.app/classes.json
Run Code Online (Sandbox Code Playgroud)
但是当我登录时fileData它会归还给我null.我不知道我在哪里做错了.
The*_*mer 25
而不是追加路径,使用pathForResource:ofType:,所以
NSString *str=[[NSBundle mainBundle] pathForResource:@"classes" ofType:@"json"];
NSData *fileData = [NSData dataWithContentsOfFile:str];
Run Code Online (Sandbox Code Playgroud)
由于某种原因,它无法读取您的文件。使用-dataWithContentsOfFile:options:error:找出原因。
NSError* error = nil;
NSData *fileData = [NSData dataWithContentsOfFile:str options: 0 error: &error];
if (fileData == nil)
{
NSLog(@"Failed to read file, error %@", error);
}
else
{
// parse the JSON etc
}
Run Code Online (Sandbox Code Playgroud)
小智 6
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"MyFile" ofType:@"txt"];
NSData *myData = [NSData dataWithContentsOfFile:filePath];
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
26044 次 |
| 最近记录: |