I'm writing JSON to disk, and that works great. But when I try to read it back, it is nil
.
Specifically, this line is nil
: NSMutableDictionary *jsonDictFromDocuments = [NSJSONSerialization JSONObjectWithData:[jsonString2 dataUsingEncoding:NSUTF8StringEncoding] options:kNilOptions error:&jsonError];
. (I tried NSDictionary *jsonDict2 = [NSJSONSerialization JSONObjectWithData:jsonData2 options:kNilOptions error:&jsonError];
too, but still got nil
.)
Every line up until that point logs the correct information from what I can tell.
// Read JSON back from disk
NSString *fileName2 = @"/myJSONFull.json";
NSLog(@"FN: %@", fileName2);
NSURL *documentsFolderURL2 = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
NSLog(@"dFURL2: %@", documentsFolderURL2);
NSString *filePath2 = [documentsFolderURL2.path stringByAppendingString:fileName2];
NSLog(@"FP2: %@", filePath2);
NSString *jsonString2 = [[NSString alloc] initWithContentsOfFile:filePath2 encoding:NSUTF8StringEncoding error:NULL];
NSLog(@"JSONs2: %@", jsonString2);
NSError *jsonError;
NSData *jsonData2 = [jsonString2 dataUsingEncoding:NSASCIIStringEncoding];
NSDictionary *jsonDict2 = [NSJSONSerialization JSONObjectWithData:jsonData2 options:kNilOptions error:&jsonError];
NSLog(@"JDFD2: %@", jsonDict2);
NSMutableDictionary *jsonDictFromDocuments = [NSJSONSerialization JSONObjectWithData:[jsonString2 dataUsingEncoding:NSUTF8StringEncoding] options:kNilOptions error:&jsonError];
NSLog(@"JDFD: %@", jsonDictFromDocuments);
Any ideas?
EDIT:
This is what I have now, but it is still nil
// Read JSON back from disk
NSString *fileName2 = @"/myJSONFull.json";
NSURL *documentsFolderURL2 = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
NSString *filePath2 = [documentsFolderURL2.path stringByAppendingString:fileName2];
NSString *jsonString2 = [[NSString alloc] initWithContentsOfFile:filePath2 encoding:NSUTF8StringEncoding error:NULL];
NSError *jsonError;
NSData *data = [NSData dataWithContentsOfFile:filePath2];
NSMutableDictionary *jsonDictFromDocuments = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&jsonError];