static0886
static0886

Reputation: 784

NSJSONSerialization messing up resulting NSDictionary

I'm having trouble parsing the json output from a web service. I am using NSJSONSerialization to parse the output into an NSDictionary. Also using AFNetworking by subclassing AFHTTPSessionManager. For now the response serialiser is AFHTTPResponseSerializer which returns NSData Here's the code I'm using:

NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:&err];

Pretty straightforward. And the err object is nil so the conversion works fine.

BUT: the result I get straight from the web service is this:

           "address":
           {
               "address1": "Ivy House",
               "address2": "Sandy Lane",
               "city": "Rush",
               "postCode": null,
               "email": "[email protected]",
               "telephone": "18437584",
               "mobile": null,
               "smsAlert": null,
               "county": "Dublin",
               "country": "Ireland",
               "websiteAddress": "www.example.com"
           },

The result I get after printing the content of dict is this:

address =     {
    address1 = "Ivy House";
    address2 = "Sandy Lane";
    city = Rush;
    country = Ireland;
    county = Dublin;
    email = "[email protected]";
    mobile = "<null>";
    postCode = "<null>";
    smsAlert = "<null>";
    telephone = 18437584;
    websiteAddress = "www.example.com";
};

The issue is that the resulting NSDictionary does NOT have double quotes and so saving the NSDictionary to disk in plist format FAILS!

I have also tried using AFJSONResponseSerializer which returns NSDictionary but the contents are the same as above!

Where's the issue here?

Thanks in advance.

Upvotes: 0

Views: 745

Answers (2)

Andrey
Andrey

Reputation: 1561

The problem here doesn't seem to be related to double quotes at all. When printed, NSDictionary (as well as other Foundation objects) drop enclosing double quotes when they are not needed (e.g. when no spaces or special characters in the string).

Now, the likely problem preventing you from serialising your NSDictionary to a property list is presence of nulls in the JSON and, consequently, in NSDictionary. According to documentation:

Property list objects include NSData, NSString, NSArray, NSDictionary, NSDate, and NSNumber objects.

Whereas nulls from JSON will be represented as instances of NSNull, thus making your NSDictionary an invalid Property List.

Upvotes: 2

cekisakurek
cekisakurek

Reputation: 2474

NSDictionary has different structure then JSON string. So when you parse a JSON string to NSDictionary you should not expect quotes.

You can save the JSON data and parse it again on runtime.

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:@"yourfilename.dat"];

 // Save it into file system
[data writeToFile:dataPath atomically:YES];

Upvotes: 1

Related Questions