如何让 NSJSONSerialization 将布尔值输出为真或假?

编程入门 行业动态 更新时间:2024-10-27 04:33:11
本文介绍了如何让 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 将布尔值输出为真或假?

本文发布于:2023-11-05 15:44:19,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1561236.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:布尔值   NSJSONSerialization

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!