如何在ios中编写json文件

如何在ios中编写json文件,ios,objective-c,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 *bo

这里我正在读写一个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答案,可在此处找到:

当然,当您第一次尝试从documents目录中读取此文件时,您不会得到任何信息,因此,如果该文件不存在,那么第一步可能是将其复制到那里


希望这能有所帮助。

在保存文件之前,您能先做一个NSLog并打印出saveBookMark吗?输出是什么?是否如预期的那样?
writeToFile:atomically:encoding:error:
error参数是否说明了什么?保存书签前,
saveBookMark
是否正确?可能是因为您使用了两种不同的编码(一种用于JSON,一种用于保存)?据我所见,中的
bookmark.JSON
作为项目中的资源添加,我认为您无法写入该文件。您可以将该文件移动到应用程序的
文档
文件夹。@IulianOnofrei哦,非常抱歉!字体使它看起来像一个小“L”。真诚的道歉。我把我的“bookmark.json”文件放在了“document”中。。。。。。谢谢你@IulianOnofrei