【问题标题】:JSON Serialization returns null from local fileJSON 序列化从本地文件返回 null
【发布时间】:2016-07-19 16:02:40
【问题描述】:

我正在将 JSON 写入磁盘,效果很好。 但是当我尝试回读时,它是nil

具体来说,这一行是nil:NSMutableDictionary *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];

【问题讨论】:

  • @vadian jsonErrornil
  • 对,问题是:你不能将NSString 传递给JSONObjectWithData (jsonString2)
  • @vadian 应该是NSData 还是什么?
  • 始终使用NSUTF8StringEncoding。也可以直接从磁盘读取数据:[NSData dataWithContentsOfFile]
  • 使用我的最后一个答案(带有 NSArray 的那个),它应该可以工作。

标签: ios objective-c json


【解决方案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);

【讨论】:

  • 感谢您在此处提供答案,以便我查看是否缺少任何内容。我得到JDFD2: (null) - error: Error Domain=NSCocoaErrorDomain Code=3840 "JSON text did not start with array or object and option to allow fragments not set." UserInfo={NSDebugDescription=JSON text did not start with array or object and option to allow fragments not set.}
  • 我试图根据这个答案添加NSJSONReadingAllowFragments stackoverflow.com/questions/14171111/… 但后来得到了JDFD2: (null) - error: Error Domain=NSCocoaErrorDomain Code=3840 "Invalid value around character 0." UserInfo={NSDebugDescription=Invalid value around character 0.}
  • @vadian:作者保存了序列化的JSON,所以它是一个plist,而且它是一个顶级的NSArray。即使您的答案应该是正确的(直接使用 dataWithContentOfURL: 而不是拖动 NSString),但在用户不保存未序列化的 JSON 之前它不会起作用。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-20
  • 2015-04-17
  • 1970-01-01
相关资源
最近更新 更多