Question

How do I get true/false (instead of "true"/"false") in json from a NSDictionary using NSJSONSerialization dataWithJSONObject? What keys should I store in the dictionary to get that?

Was it helpful?

Solution

NSNumber objects containing a BOOL are mapped to JSON "true" and "false". So just use @YES, @NO, or generally @(someBOOL). For example:

NSDictionary *dict = @{@"this is": @YES};
NSData *data = [NSJSONSerialization dataWithJSONObject:dict options:0 error:NULL];
NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
// {"this is":true}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top