如何将数据写入JSON数据文件?

时间:2014-04-29 22:47:42

标签: objective-c json database nsdictionary

我是Xcode和Objective-C的新手,所以请原谅我这是非常容易的。我正在使用JSON数据库中的字典,我想为一个人填写一个名为“age”的数据库元素。 JSON文件中的数据如下所示:

{

"firstName":"Debra",
"lastName":"Palmer",
"middleInitial":"A",
"firstNameFirst":"Debra A. Palmer",
"lastNameFirst":"Palmer, Debra A.",
"age":""

}

这是我的代码:

      for(NSMutableDictionary *person in _contactsArray1)
        {
            if([person[@"firstNameFirst"] isEqual: _fullName] && [person[@"age"] isEqual: @""])
            {
                person[@"age"] = _ageString;
                // send age data to the dictionary, I get an error saying
                // Error: Expected method to write dictionary element not found on object type of NSDictionary
            }
            else if ([person[@"firstNameFirst"] isEqual: _fullName] && ![person[@"age"] isEqual: @""])
            {

            }
        }

这是我现在加载JSON数据的代码:

       NSString *filePath1 = [[NSBundle mainBundle] pathForResource:@"contacts" ofType:@"json"];

    // Load the file into an NSData object called JSONData

    NSError *error1 = nil;

    NSMutableData *JSONData1 = [NSMutableData dataWithContentsOfFile:filePath1 options:NSDataReadingMappedIfSafe error:&error1];

    // Create an Objective-C object from JSON Data

    _contactsArray1 = [NSJSONSerialization
                  JSONObjectWithData:JSONData1
                  options:0
                  error:

0 个答案:

没有答案