Question

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?

Was it helpful?

Solution

JSON "true" and "false" values are stored as NSNumber objects, so the following should work:

BOOL b = [deserializedDictionary[@"GetHoroscope"] boolValue];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top