English 中文(简体)
Copying JSON response dictionary to plist
原标题:

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?

问题回答

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)





相关问题
JQuery/MVC Search Issue

I have inherited a piece of work where the entry screen shows a summary of 20 calculated variables. E.g. Var A (250), Var B (79). Clicking on any of these links takes the user to a view with a ...

jQuery quicksearch plug-in tinkering with JSON

I ve implemented the quicksearch plugin by Rik Lomas and I love it for an application in a custom CMS I m building. I was wondering though, since I m going to have a bizillion items in the table if ...

JSON with classes?

Is there a standardized way to store classes in JSON, and then converting them back into classes again from a string? For example, I might have an array of objects of type Questions. I d like to ...

PHP json_decode question

i m trying to use json_decode to combine a few json objects and then re-encode it. my json looks like: { "core": { "segment": [ { "id": 7, "...

Converting JSON data to Java object

I want to be able to access properties from a JSON string within my Java action method. The string is available by simply saying myJsonString = object.getJson(). Below is an example of what the string ...

热门标签