I'm using Apple's relatively new Foundation class NSJSONSerialization to convert JSON data coming from the Rotten Tomatoes web service. I'm having trouble with empty number fields (e.g., the year field is sometimes empty).
Setup: Xcode 4.2, ARC, Rotten Tomatoes API is a RESTful web service
Here's the conversion using the aforementioned class:
If I log the json dictionary object, I can see an empty year field (note: this is just a very small portion of the logged NSDictionary object):
But how is empty represented? By an NSNull object? However, the following conditional will not evaluate to YES for that empty year field (note: aMovie is a dictionary object that I parsed out of the json dictionary object; it contains all the information about a single movie):
If I execute the following code an exception is thrown because the year field is empty:
So what I have been doing is catching that exception (i.e., @try and @catch), but there has got to be a better way to handle an empty number field.
Thanks.
Setup: Xcode 4.2, ARC, Rotten Tomatoes API is a RESTful web service
Here's the conversion using the aforementioned class:
Code:
NSError *error;
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:self.receivedData options:0 error:&error];
If I log the json dictionary object, I can see an empty year field (note: this is just a very small portion of the logged NSDictionary object):
Code:
title = "Heart of the Lion (Coeur de lion)";
year = "";
But how is empty represented? By an NSNull object? However, the following conditional will not evaluate to YES for that empty year field (note: aMovie is a dictionary object that I parsed out of the json dictionary object; it contains all the information about a single movie):
Code:
if ([aMovie objectForKey:@"year"] == [NSNull null])
NSLog(@"year is null!");
If I execute the following code an exception is thrown because the year field is empty:
Code:
NSNumber *year = [aMovie objectForKey:@"year"];
result.year = [year stringValue]; // throws exception
So what I have been doing is catching that exception (i.e., @try and @catch), but there has got to be a better way to handle an empty number field.
Thanks.
Last edited: