JSON序列化从本地文件返回null

时间:2016-07-19 16:02:41

标签: ios objective-c json

我正在将JSON写入磁盘,这很有效。 但是当我尝试阅读它时,它是nil

具体来说,这一行是nilNSMutableDictionary *jsonDictFromDocuments = [NSJSONSerialization JSONObjectWithData:[jsonString2 dataUsingEncoding:NSUTF8StringEncoding] options:kNilOptions error:&jsonError];。 (我也试过了NSDictionary *jsonDict2 = [NSJSONSerialization JSONObjectWithData:jsonData2 options:kNilOptions error:&jsonError];,但仍然得到nil。)

在此之前的每一行都记录了我能说的正确信息。

// 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);

有什么想法吗?

编辑:

这就是我现在所拥有的,但仍然是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];

1 个答案:

答案 0 :(得分:1)

请尝试此操作,它使用与URL相关的API并在反序列化中记录可能的错误。

NSString *fileName2 = @"myJSONFull.json";
NSLog(@"FN: %@", fileName2);

NSURL *documentsFolderURL2 = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask] lastObject];
NSLog(@"dFURL2: %@", documentsFolderURL2);

NSURL *fileURL = [documentsFolderURL2 URLByAppendingPathComponent:fileName2];
NSLog(@"FP2: %@", fileURL);

NSData *jsonData = [NSData dataWithContentsOfURL:fileURL];

NSError *jsonError;
NSDictionary *jsonDict2 = [NSJSONSerialization JSONObjectWithData:jsonData options:kNilOptions error:&jsonError];
NSLog(@"JDFD2: %@ - error: %@", jsonDict2, jsonError);

修改:

因为你的 JSON 实际上是PLIST -
错误消息 JSON文本未启动... 表示这不是JSON -
使用适当的序列化类:

...

NSLog(@"FP2: %@", fileURL);
NSData *plistData = [NSData dataWithContentsOfURL:fileURL];

NSError *plistError;
NSArray *plistArray = (NSArray *)[NSPropertyListSerialization propertyListWithData:plistData
                                                             options:NSPropertyListImmutable
                                                              format:nil
                                                               error:&plistError];
NSLog(@"JDFD2: %@ - error: %@", plistArray, jsonError);