Ios 从本地JSON文件加载数据

Ios 从本地JSON文件加载数据,ios,objective-c,json,Ios,Objective C,Json,使用断点,我发现每个对象都在初始化,但字典值是nil。语法有什么问题吗 NSString *fileResource = @"albums"; NSString *filePath = [[NSBundle mainBundle]pathForResource:fileResource ofType:@"rtf"]; NSLog(@"%@",filePath); NSURL *localFileURL = [NSURL fileURLWithPath:filePath]; NSData *con

使用断点,我发现每个对象都在初始化,但字典值是
nil
。语法有什么问题吗

NSString *fileResource = @"albums";
NSString *filePath = [[NSBundle mainBundle]pathForResource:fileResource ofType:@"rtf"];
NSLog(@"%@",filePath);
NSURL *localFileURL = [NSURL fileURLWithPath:filePath];
NSData *contentOfLocalFile = [NSData dataWithContentsOfURL:localFileURL];
NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:contentOfLocalFile options:0 error:nil];
JSON文件如下所示

{
    "Albums": [{
        "Title": "Album 1",
        "Desc": "Description",
        "images": ["http://img0.etsystatic.com/000/0/6391580/il_fullxfull.340668108.jpg", "https://img1.etsystatic.com/000/0/6515756/il_fullxfull.330077675.jpg", "http://www.betterphoto.com/uploads/processed/0724/0706140402101moore_5-2.jpg"]
    }, {
        "Title": "Album 2",
        "Desc": "Description",
        "images": ["http://www.betterphoto.com/uploads/processed/0724/0706140402101moore_5-2.jpg", "http://www.artinnaturephotography.com/images/large/06012008_001.jpg",
            "http://img0.etsystatic.com/000/0/6391580/il_fullxfull.340668108.jpg", "https://img1.etsystatic.com/000/0/6515756/il_fullxfull.330077675.jpg", "http://www.betterphoto.com/uploads/processed/0724/0706140402101moore_5-2.jpg"
        ]
    }]
}
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"fileName" ofType:@"json"];

NSString *myJSON = [[NSString alloc] initWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:NULL];

NSError *error =  nil;

//// if it is array
NSArray *jsonDataArray = [NSJSONSerialization JSONObjectWithData:[myJSON dataUsingEncoding:NSUTF8StringEncoding] options:kNilOptions error:&error];

试试这个,我应该知道你使用的是rtf而不是json

 NSString *filePath = [[NSBundle mainBundle] pathForResource:@"albums" ofType:@"json"];
    NSData *JSONData = [NSData dataWithContentsOfFile:filePath options:NSDataReadingMappedIfSafe error:nil];

    NSError *readerror;

    id jsonObject = [NSJSONSerialization JSONObjectWithData:JSONData options:NSJSONReadingMutableContainers error:&readerror];

要将本地JSON文件加载到项目中,请使用默认的GeoJson文件,而不是使用rtf文件

您可以像这样检索数据

{
    "Albums": [{
        "Title": "Album 1",
        "Desc": "Description",
        "images": ["http://img0.etsystatic.com/000/0/6391580/il_fullxfull.340668108.jpg", "https://img1.etsystatic.com/000/0/6515756/il_fullxfull.330077675.jpg", "http://www.betterphoto.com/uploads/processed/0724/0706140402101moore_5-2.jpg"]
    }, {
        "Title": "Album 2",
        "Desc": "Description",
        "images": ["http://www.betterphoto.com/uploads/processed/0724/0706140402101moore_5-2.jpg", "http://www.artinnaturephotography.com/images/large/06012008_001.jpg",
            "http://img0.etsystatic.com/000/0/6391580/il_fullxfull.340668108.jpg", "https://img1.etsystatic.com/000/0/6515756/il_fullxfull.330077675.jpg", "http://www.betterphoto.com/uploads/processed/0724/0706140402101moore_5-2.jpg"
        ]
    }]
}
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"fileName" ofType:@"json"];

NSString *myJSON = [[NSString alloc] initWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:NULL];

NSError *error =  nil;

//// if it is array
NSArray *jsonDataArray = [NSJSONSerialization JSONObjectWithData:[myJSON dataUsingEncoding:NSUTF8StringEncoding] options:kNilOptions error:&error];

使用
JSONObjectWithData:options:error:
方法中的
error
属性,查看错误是什么。添加NSError*error;[NSJSONSerialization JSONObjectWithData:ContentOfCalFile选项:0错误:错误];它将显示错误内容。@rmaddy它显示数据无法读取,因为它的格式不正确。然后修复JSON文件,使其成为有效的JSON。顺便说一句,为什么要将RTF文件视为JSON文件?它们是两种完全不同的文件。@rmaddy你能告诉我怎么做吗。我应该把扩展名重命名为.json吗?