views:

270

answers:

1

I have a dictionary containing a JSON response and a plist file. I want to update the values in my plist file with the JSON response values. How would I do this?

+2  A: 

this is what i did, im working on it now, but im getting there:


JSON to dictionary:

NSString *jsonString = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding]; //NSLog(@"%@",jsonString);

NSArray *result = [jsonString JSONValue];

for(NSDictionary *dictionary in result){
 return dictionary; //if you are getting more then one row, do something here
}


Saving the dictionary:

id plist = plistDict;

NSString *errorDesc;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0]; 
NSString *plistPath = [documentsDirectory stringByAppendingPathComponent:@"Data.plist"];
NSLog(@"%@",plistPath);

NSData *xmlData;
NSString *error;

xmlData = [NSPropertyListSerialization dataFromPropertyList:plist
              format:NSPropertyListXMLFormat_v1_0
             errorDescription:&error];
if(xmlData) {
 if ([xmlData writeToFile:plistPath atomically:YES]) {
  NSLog(@"Data successfully saved.");
 }else {
  NSLog(@"Did not managed to save NSData.");
 }

}
else {
 NSLog(@"%@",errorDesc);
 [error release];
}

}


if you want to update values, I would say you should open the plist, place it in a dictionary, update the value in the dictionary, and save the dictionary to the plist again.

Hope this helps.

Cyrille Derche
Nice first post. ;)Welcome to SO!
Pascal