问题
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?
回答1:
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}
来源:https://stackoverflow.com/questions/22411119/how-to-create-true-false-in-json-in-objective-c