Reputation: 45921
I'm developing an iOS 5 and above application with latest SDK.
I have to parse this JSON:
{"GetHoroscope":false,"GetQuoteOfTheDay":false, ... }
To do it, I have this code:
- (NSDictionary*)getDictionaryFromNSData:(NSData*)jsonData
{
NSError* error = nil;
id jsonObject = [NSJSONSerialization
JSONObjectWithData:jsonData
options:NSJSONReadingAllowFragments
error:&error];
if ((jsonObject != nil) && (error == nil))
{
NSLog(@"Successfully deserialized...");
if ([jsonObject isKindOfClass:[NSDictionary class]])
{
NSDictionary* deserializedDictionary = (NSDictionary *)jsonObject;
NSLog(@"Dersialized JSON Dictionary = %@", deserializedDictionary);
return deserializedDictionary;
}
}
return nil;
}
But I have a problem with boolean
values. When I check deserializedDictionary
I see that GetHoroscope
and GetQuoteOfTheDay
values are null.
Do I need to do something special with boolean values?
Upvotes: 3
Views: 3626
Reputation: 539775
JSON "true" and "false" values are stored as NSNumber
objects, so the following
should work:
BOOL b = [deserializedDictionary[@"GetHoroscope"] boolValue];
Upvotes: 9