将JSON响应写入.plist文件
挫折的顶部!
我正在从服务获取一些JSON响应 ,我想将其存储在.plist文件中以供将来参考。 我无法将我的JSON响应保存到.plist文件 。 我认为这是由于一些null
值进入响应。
注意: 我确认响应是使用 jsonparser的 JSON格式 。
我的代码:
NSError *error; NSDictionary* json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error]; NSDictionary *dictResult = [(NSDictionary*)json objectForKey:@"myKey"]; NSLog(@"Result Dictionary :: %@",dictResult); NSURL *cacheDir = [[[NSFileManager defaultManager] URLsForDirectory:NSCachesDirectory inDomains:NSUserDomainMask] lastObject]; NSURL *path = [cacheDir URLByAppendingPathComponent:@"FinalResult.plist"]; NSLog(@"Path :: %@",path); BOOL success = [dictResult writeToURL:path atomically:YES]; NSLog(@"success? %d", success);
注意: 我得到了所有的NSLog值 (意味着响应字典和文件path,但0成功 ) 。
问题:响应中有几乎70-80个键值对 ,我不想删除/replace所有的null
值。 因为我想要的是…
- 从服务器获取响应。
- 用响应填充所有的UITextFields 。
- 使用来自UITextFields的一些编辑值向服务器发送相同的响应。
所以,我只是想更改对象中的编辑的UITextField
值,并让它POST到服务器。
什么是解决这个问题的最佳方法?
我敢打赌,你的JSON包含至less一个null
值。
当你有包含null
JSON并且使用NSJSONSerialization
转换时, null
被NSNull
的一个实例NSNull
。 如果你的字典包含NSNull
,那么writeToURL:atomically:
会失败。
这是因为用于读写字典(和数组)的便利方法仅在集合中的数据限于属性列表types时才起作用。 这些是:
-
NSString
-
NSNumber
-
NSData
-
NSDate
-
NSArray
-
NSDictionary
。 对于字典,键必须是NSString
s。
写时,你也可以使用可变的子类(比如NSMutableString
)。
如果你有任何不在列表中,你不能使用writeToURL:atomically
或任何类似的便利方法。
问题是一些有效的JSON不能转换为属性列表。 此外,一些有效的属性列表不能转换为JSON(因为NSDate
不会自动转换为有效的JSON)。
如果是我,我只是写数据到一个JSON文件。 保留原来的格式。 你可以很容易地转换到/从JSON,所以要这样。
如果你的字典包含NSNull
,那么writeToURL:atomically
:会失败。
对于字典, 键必须是NSStrings 。
问题是一些有效的JSON不能转换为属性列表。 此外,一些有效的属性列表不能转换为JSON 。
不要忘记,如果您必须使用属性列表,您将需要扫描整个字典并将空值转换为可以保存在属性列表文件中的东西。
唯一的解决办法是你必须检查所有的NULL值并用@“”replace。
快乐编码
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES); NSString *libraryPath = [paths objectAtIndex:0]; NSString *filename = @"FinalResult.plist"; NSString *pathFilename = [libraryPath stringByAppendingPathComponent:filename]; NSDictionary *dictResult = [json objectForKey:@"myKey"]; [dictResult writeToFile:pathFilename atomically:YES];
我以这种方式构build我的文件URL:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES); NSString *path = [paths objectAtIndex:0]; NSString *filename = @"FinalResult.plist"; NSString *pathFilename = [path stringByAppendingPathComponent:filename];
然后看看是否写道:
BOOL success = [dictResult writeToFile:pathFilename atomically:YES]; NSLog(@"success? %d", success);
编辑 – 搞笑,我刚刚面对这个问题,然后忘了这一切。 一些JSONparsing器将使用[NSNull null]作为nil值的占位符。 我写了这个(认真,大约两个星期前,然后间隔)清理parsing结果…
- (NSDictionary *)compact:(NSDictionary *)aDictionary { NSDictionary *answer = [NSMutableDictionary dictionary]; for (NSString *key in [aDictionary allKeys]) { id value = [self.dictionary valueForKey:key]; if (value && value != [NSNull null]) { [answer setValue:value forKey:key]; } } return [NSDictionary dictionaryWithDictionary:answer]; }
这可以做成一个NSDictionary类别添加如果你想。
- 检查
writeToURL:atomically:
方法的返回值是否返回YES。 - 如果没有,请检查您是否具有对该URL的写入权限。
- 尝试使用另一个URL来查找是否导致此错误的path或字典的内容。
试试这个,它会起作用:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; NSString *path = [documentsDirectory stringByAppendingPathComponent:@"country.plist"]; [jsonData writeToFile:path atomically:YES];