Write JSON Response to .plist File

前端 未结 6 1602
遥遥无期
遥遥无期 2021-02-20 11:14

Frustration on the Top !!!

I am getting some JSON Response from the Service and I want to store it in the .plist file

相关标签:
6条回答
  • 2021-02-20 11:50
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
    NSString *libraryPath = [paths objectAtIndex:0];
    NSString *filename = @"FinalResult.plist";
    NSString *pathFilename = [libraryPath stringByAppendingPathComponent:filename];
    
    NSDictionary *dictResult = [json objectForKey:@"myKey"];
    [dictResult writeToFile:pathFilename atomically:YES];
    
    0 讨论(0)
  • 2021-02-20 11:50

    I build my file urls this way:

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
    NSString *path = [paths objectAtIndex:0];
    NSString *filename = @"FinalResult.plist";
    NSString *pathFilename = [path stringByAppendingPathComponent:filename];
    

    Then see if this writes:

    BOOL success = [dictResult writeToFile:pathFilename atomically:YES];
    NSLog(@"success? %d", success);
    

    Edit - Funny, I just recently confronted this problem and then forgot all about it. Some JSON parsers will use [NSNull null] as placeholders for nil values. I wrote this (seriously, just about two weeks ago and then spaced on it) to clean up the parse result...

    - (NSDictionary *)compact:(NSDictionary *)aDictionary {
    
        NSDictionary *answer = [NSMutableDictionary dictionary];
    
        for (NSString *key in [aDictionary allKeys]) {
            id value = [self.dictionary valueForKey:key];
            if (value && value != [NSNull null]) {
                [answer setValue:value forKey:key];
            }
        }
        return [NSDictionary dictionaryWithDictionary:answer];
    }
    

    This could be made into a NSDictionary category addition if you wanted.

    0 讨论(0)
  • 2021-02-20 12:03

    I bet that your JSON contains at least one null value.

    When you have JSON that contains null and you convert it using NSJSONSerialization, the null is replaced by an instance of NSNull. If your dictionary contains NSNull, then writeToURL:atomically: will fail.

    This is because the convenience methods for reading and writing dictionaries (and arrays) only work if the data in the collection is restricted to property list types. These are:

    • NSString
    • NSNumber
    • NSData
    • NSDate
    • NSArray
    • NSDictionary. And for dictionaries, the keys must be NSStrings.

    You can also use mutable subclasses (like NSMutableString) when writing.

    If you have anything not on that list, you can't use writeToURL:atomically or any of the similar convenience methods.

    The problem is that some valid JSON can't be converted to property lists. Also, some valid property lists can't be converted to JSON (because NSDate won't automatically convert to valid JSON).

    If it was me, I'd just write the data to a JSON file. Leave it in its original format. You can convert to/from JSON easily, so leave it that way.

    0 讨论(0)
  • 2021-02-20 12:10
    • Check if the return value of the writeToURL:atomically: method returns YES.
    • If it doesn't, check if you have write permissions to that URL.
    • Try it with another URL to find out if its the path or the contents of the dictionary that's causing this error.
    0 讨论(0)
  • 2021-02-20 12:12

    If your dictionary contains NSNull, then writeToURL:atomically: will fail.

    For dictionaries, the keys must be NSStrings.

    The problem is that some valid JSON can't be converted to property lists. Also, some valid property lists can't be converted to JSON.

    Don't Forget, If you must use a property list, you will need to scan the entire dictionary and convert the nulls into something that can be saved in a property list file.

    Only Solution is that you have to check all the NULL Values and Replace it with @" ".

    Happy Coding...

    0 讨论(0)
  • 2021-02-20 12:16

    Try this, it will work :

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *path = [documentsDirectory stringByAppendingPathComponent:@"country.plist"];
    [jsonData writeToFile:path atomically:YES];
    
    0 讨论(0)
提交回复
热议问题