Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create true/false in JSON in Objective-C

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?

like image 415
Boon Avatar asked Mar 14 '14 16:03

Boon


1 Answers

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}
like image 114
Martin R Avatar answered Oct 06 '22 01:10

Martin R