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

萝らか妹 提交于 2019-12-30 10:15:11

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!