NSJSONSerialization unboxes NSNumber?

我正在使用NSJSONSerializationJSON文档转换为Core Foundationtypes。

我有一个字段在我的JSON这是一个“数字”。 有时是整数,有时候是浮点数。

现在,问题是当NSJSONSerialization把我的JSON变成一个NSDictionary ,我试图提取使用objectForKey的数字,有时它是一个int,有时它是一个双。 看来NSJSONSerialization并不是简单的把它放在一个NSNumber包装器中,而是实际上将这个值解开,并把插入到NSDictionary

很奇怪。 我以为你应该把原语放进一个NSDictionary 。 所以我现在的问题是,我不知道什么types(INT或双)我应该铸造来保存实际的数值。

任何人都知道一个出路?

你可以从NSNumber获取原始types。 只需使用以下代码片段即可:

  const char* type = [theValue objCType]; if (strcmp (type, @encode (NSInteger)) == 0) { //It is NSInteger } else if (strcmp (type, @encode (NSUInteger)) == 0) { //It is NSInteger } else if (strcmp (type, @encode (int)) == 0) { //It is NSUInteger } else if (strcmp (type, @encode (float)) == 0) { //It is float } else if (strcmp (type, @encode (double)) == 0) { //It is double } else if (strcmp (type, @encode (long)) == 0) { //It is long } else if (strcmp (type, @encode (long long)) == 0) { //It is long long } 

等等。

原来他们一直都是NSNumbers,而我只是在debugging方面没有经验。 我只是困惑,认为他们是int和double值,因为这是debugging器如何呈现它们。 所以这是debugging器拆箱,而不是NSJSONSerialization …