我怎么NSJSONSerialization输出一个布尔值,真或假? [英] How do I get NSJSONSerialization to output a boolean as true or false?

查看:306
本文介绍了我怎么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?

推荐答案

没有,基金会对象为布尔是的NSNumber numberWithBool ,成为0或1。我们没有任何布尔对象。同去阅读 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.

您可以创建一个布尔类,并建立自己的解析器。数组是数组和 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天全站免登陆