Copying JSON response dictionary to plist Copying JSON response dictionary to plist json json

Copying JSON response dictionary to plist


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.


If you are working under Mac OS X 10.7 or iOS 5, there is a Foundation class called NSJSONSerialization that will read/write JSON files. Converting JSON to plist will be as simple as: (Implying you have ARC or GC on)

NSString *infile = @"/tmp/input.json"NSString *oufile = @"/tmp/output.plist"[[NSJSONSerialization JSONObjectWithData:[NSData dataWithContentsOfFile:infile]                                 options:0                                   error:NULL] writeToFile:oufile                                                atomically:YES];

However a conversion from plist to JSON will be more troublesome since NSDate and NSData objects cannot appear in JSONs. You may need to check the contents of the file and store the NSData and NSDate in an alternate way (like NSData as Base-64 strings and NSDate as their UNIX times)