Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Write JSON Response to .plist File

Frustration on the Top !!!

I am getting some JSON Response from the Service and I want to store it in the .plist file for Future Reference. I am unable to save my JSON Response to .plist File. I think it's due to some null values into the Response.

Note : I confirmed that the Response is in JSON Format using jsonparser.


My Code :

NSError *error;
NSDictionary* json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
NSDictionary *dictResult = [(NSDictionary*)json objectForKey:@"myKey"];
NSLog(@"Result Dictionary :: %@",dictResult);

NSURL *cacheDir = [[[NSFileManager defaultManager] URLsForDirectory:NSCachesDirectory inDomains:NSUserDomainMask] lastObject];
NSURL *path = [cacheDir URLByAppendingPathComponent:@"FinalResult.plist"];
NSLog(@"Path :: %@",path);

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

Note : I got all the NSLog Values (means the Response Dictionary and File Path but 0 for success).


Problem : There are almost 70-80 key-value pairs in the Response and I don't want to remove/replace all the null values. Because What I want is ...

  1. GET the Response From the Server.
  2. Fill all the UITextFields with the Response.
  3. POST the Same Response to the Server with some Edited Values from the UITextFields.

So, I just want to change the Edited UITextField values in the Object and let it POST to the Server.

What is the Optimum Way to Fix this ?

like image 999
Bhavin Avatar asked Mar 20 '13 14:03

Bhavin


3 Answers

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.

like image 96
Tom Harrington Avatar answered Nov 17 '22 14:11

Tom Harrington


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...

like image 11
Jignesh.Raj Avatar answered Nov 17 '22 15:11

Jignesh.Raj


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];
like image 3
Dipen Panchasara Avatar answered Nov 17 '22 15:11

Dipen Panchasara