ios - 如何在ios中写入json文件

标签 ios objective-c json

我在这里读写一个json文件。

读取已正确完成,但当我写入文件时,它不会在 json 文件中写入数据。

这是我的代码。

//reading Json file....
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"bookmark" ofType:@"json"];

NSData *content = [[NSData alloc] initWithContentsOfFile:filePath];
NSArray *bookmarkJson=[NSJSONSerialization JSONObjectWithData:content options:0 error:nil];
//this contains array's of dictionary....
NSDictionary *newBookmark=@{@"index":@"1.1.1.1",@"text":@"Header",@"htmlpage":@"page_name"};

//take new array to add data with previous one
NSMutableArray *temp=[[NSMutableArray alloc]initWithArray:bookmarkJson];
// add object to new array...
[temp insertObject:newBookmark atIndex:0];

//now serialize temp data....
NSData *serialzedData=[NSJSONSerialization dataWithJSONObject:temp options:0 error:nil];

NSString *saveBookmark = [[NSString alloc] initWithBytes:[serialzedData bytes] length:[serialzedData length] encoding:NSUTF8StringEncoding];

//now i write json file.....    
[saveBookmark writeToFile:@"bookmark.json" atomically:YES encoding:NSUTF8StringEncoding error:nil];

在“saveBookmark”(NSString)对象中,我得到了正确的文件格式,但在 bookmark.json 文件中,我没有得到任何新值。

请帮我解决这个问题......

最佳答案

编辑:正如@IulianOnofrei 正确指出的那样,使用文档目录而不是资源目录来读/写文件。

使用这些方法读写数据,你的问题应该解决了:

- (void)writeStringToFile:(NSString*)aString {

    // Build the path, and create if needed.
    NSString* filePath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString* fileName = @"bookmark.json";
    NSString* fileAtPath = [filePath stringByAppendingPathComponent:fileName];

    if (![[NSFileManager defaultManager] fileExistsAtPath:fileAtPath]) {
        [[NSFileManager defaultManager] createFileAtPath:fileAtPath contents:nil attributes:nil];
    }

    // The main act...
    [[aString dataUsingEncoding:NSUTF8StringEncoding] writeToFile:fileAtPath atomically:NO];
}

- (NSString*)readStringFromFile {

    // Build the path...
    NSString* filePath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString* fileName = @"bookmark.json";
    NSString* fileAtPath = [filePath stringByAppendingPathComponent:fileName];

    // The main act...
    return [[NSString alloc] initWithData:[NSData dataWithContentsOfFile:fileAtPath] encoding:NSUTF8StringEncoding];
}

代码来自此处找到的另一个 SO 答案:Writing and reading text files on the iPhone

当然,第一次尝试从文档目录读取此文件时,您不会得到任何东西,因此如果文件不存在,第一步可能是将文件复制到那里。

希望这对您有所帮助。

关于ios - 如何在ios中写入json文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31852949/

相关文章:

ios - 如何导致模型更新以快速触发另一个 Controller 中的更新

iphone - 将 URL(作为字符串)添加到数组时出现问题

javascript - 定义 JSON 以满足需求

java - Jackson 将类名序列化为所有对象的属性

ios - 具有多个循环的 Json 解析

ios - 从加速度计执行操作

ios - 关闭并重新打开应用程序

ios - 如何在这样的代码中绘制可调整大小的 UIImage?

objective-c - - 谁应该负责调用 runModalForWindow - Controller 或初始化 Controller 的人

objective-c - 在 initWithNibName 中调用 addSubview : causes viewDidLoad (and other UI Object inits)to fire before the addSubview Call executes