How do I get NSJSONSerialization to output a boolean as true or false?(如何让 NSJSONSerialization 将布尔值输出为真或假?)
问题描述
我正在使用 NSJSONSerialization dataWithJSONObject 将我的类序列化为 JSON.当它序列化一个 BOOL 时,它会在 JSON 字符串中为其提供值 1 或 0.我需要这是真或假.这可以通用吗?
I'm using NSJSONSerialization dataWithJSONObject to serialize my classes to JSON. When it serializes a BOOL, it gives it the value 1 or 0 in the JSON string. I need this to be true or false instead. Is this possible to do generically?
推荐答案
不,Bool 的基础对象是 NSNumber numberWithBool,它变成 0 或 1.我们没有 Bool 对象.阅读 JSON 也是如此.True/false 将再次变为 NSNumber.
No, the Foundation Object for a Bool is NSNumber numberWithBool, which becomes either 0 or 1. We have no Bool Object. Same go for reading JSON. True/false will become a NSNumber again.
您可以创建一个 Bool 类并构建自己的解析器.数组是数组,JSON 对象是 NSDictionary.您可以查询密钥,测试其背后的类并从中构建 JSON 字符串.
You could create an Bool Class and build your own parser. Arrays are Arrays and JSON Objects are NSDictionary. You can query the keys, test what Class lies behind and build the JSON String from this.
这篇关于如何让 NSJSONSerialization 将布尔值输出为真或假?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何让 NSJSONSerialization 将布尔值输出为真或假?
- GPS状态的广播接收器? 2022-01-01
- UITextView 内容插图 2022-01-01
- Xcode 7.3 中带有 UILabel 的 UIStackView 2022-01-01
- 使用自动布局向 UIScrollView 添加动态大小的视图 2022-01-01
- 在 Iphone SDK 的导航栏上添加多个按钮 2022-01-01
- 如何在 iPhone 模拟器中重置 NSUserDefaults 数据? 2022-01-01
- URL编码Swift iOS 2022-01-01
- 网上有没有好的 UIScrollView 教程? 2022-01-01
- SetOnItemSelectedListener上的微调程序错误 2022-01-01
- 类似于 Mail.app 的 iPad 模态视图控制器? 2022-01-01
