【发布时间】:2012-10-03 07:32:28
【问题描述】:
我最近开始在 MAC OS 10.6 上进行应用程序开发,我正在尝试使用 SBJSON 在我的 MAC 机器上修改本地 JSON 文件中的“键/值”对。我已成功读取键的值,但我无法了解如何修改键的值并将其同步到 JSON 文件。假设,我在本地文件中有以下 JSON 数据:
{
"name": {
"fName":"John",
"lName":"Doe"
}
}
我想将“fName”的值更改为其他值,例如 Robert。
我已经尝试了很多搜索,但没有任何线索......谁能帮助我。
我正在使用 SBJSON 框架!
代码:
NSString *filePath = @"/Users/dev/Desktop/SQLiteFile/myJSON2.json";
NSData *myData = [NSData dataWithContentsOfFile:filePath];
NSString *responseString = [[NSString alloc] initWithData:myData encoding:NSUTF8StringEncoding];
NSLog(@"FILE CONTENT : %@", responseString);
SBJsonParser *jsonParser = [[SBJsonParser alloc] init];
NSDictionary * dictionary = (NSDictionary*)[jsonParser objectWithString:responseString error:NULL];
[dictionary setObject:@"Robert" forKey:@"fName"];
//
// Code for writing this change into the file, which i needed.
//
[jsonParser release];
【问题讨论】:
-
您能否修改您的问题以显示一些代码来演示您如何将 JSON 数据转换为 NSDictionary(或用于提取“
fName”值的任何类型)?如果你能做到这一点,我(或其他人)也许能够更轻松地帮助你想出一个解决方案。 -
嗨迈克尔!我添加了代码。我在其中获取 JSON 数据并将其转换为字典,然后修改字典。我希望这些更改在我的本地 JSON 文件中受到影响。
标签: objective-c sbjson