【问题标题】:how to write json file in ios如何在ios中写入json文件
【发布时间】:2015-08-06 10:09:53
【问题描述】:

这里我正在读写一个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 文件中我没有得到任何新值。

请帮帮我……

【问题讨论】:

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

标签: ios objective-c json


【解决方案1】:

编辑:正如@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

当然,当您第一次尝试从文档目录中读取此文件时,您将一无所获,所以如果该文件不存在,第一步可能是将其复制到那里。

希望这会有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-19
    • 1970-01-01
    • 1970-01-01
    • 2020-09-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多