如何在Objective-C中的JSON中创建true/false [英] How to create true/false in JSON in Objective-C

查看:143
本文介绍了如何在Objective-C中的JSON中创建true/false的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用NSJSONSerialization dataWithJSONObject从NSDictionary中获取json中的true/false(而不是"true"/"false")?我应该在字典中存储哪些键才能得到它?

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?

推荐答案

NSNumber包含BOOL的对象被映射到JSON"true"和"false". 因此,只需使用@YES@NO或通常使用@(someBOOL).例如:

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}

这篇关于如何在Objective-C中的JSON中创建true/false的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆