如何最简单地编写JSON数据/NSDictionary并再次读取它?我知道有NSFileManager,但是是否有开源框架库可以使这个过程更容易?iOS5的NSJSONSerialization类是否支持将数据写入磁盘?
如何最简单地编写JSON数据/NSDictionary并再次读取它?我知道有NSFileManager,但是是否有开源框架库可以使这个过程更容易?iOS5的NSJSONSerialization类是否支持将数据写入磁盘?
没错,NSJSONSerialization
是一个好的选择:
NSString *fileName = @"myJsonDict.dat"; // probably somewhere in 'Documents'
NSDictionary *dict = @{ @"key" : @"value" };
NSOutputStream *os = [[NSOutputStream alloc] initToFileAtPath:fileName append:NO];
[os open];
[NSJSONSerialization writeJSONObject:dict toStream:os options:0 error:nil];
[os close];
// reading back in...
NSInputStream *is = [[NSInputStream alloc] initWithFileAtPath:fileName];
[is open];
NSDictionary *readDict = [NSJSONSerialization JSONObjectWithStream:is options:0 error:nil];
[is close];
NSLog(@"%@", readDict);
+ writeJSONObject:toStream:options:error:
时,您的应用程序将挂起并消耗100%的CPU。文件本身将由流创建。