使用断点我发现每个对象都被初始化但字典值是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"
]
}]
}
答案 0 :(得分:0)
尝试这个我应该工作你使用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];
答案 1 :(得分:-1)
要将本地JSON文件加载到项目中,请使用默认的GeoJson文件,而不是使用rtf文件。GeoJson
您可以像这样检索数据
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];