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 将布尔值输出为真或假?


基础教程推荐
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01