如何让 NSJSONSerialization 将布尔值输出为真或假? [英] How do I get NSJSONSerialization to output a boolean as true or false?

查看:23
本文介绍了如何让 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 将布尔值输出为真或假?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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