2012-11-07 63 views
0
for (NSArray *values in [serializedJSON allValues]) 

有时,serializedJSON中的值将是数组,有时它们将是NSDictionaries。我想歧视其中的一个,所以我没有像现在这样得到任何错误。所以我只希望在这种情况下返回的值是NSArrays,而在第二种情况下,我只希望它们是NSDictionaries。在遍历NSDictionary的同时处理不同的对象类型

感谢先进!

如果您需要更多的信息让我知道

+1

[isKindOfClass](http://developer.apple.com/library /ios/documentation/Cocoa/Reference/Foundation/Protocols/NSObject_Protocol/Reference/NSObject.html#//apple_ref/occ/intfm/NSObject/isKindOfClass :) –

+0

你无法控制JSON映射的内容,你只能做通过测试返回的内容来控制你对它的反应。 –

回答

4

的标准,来处理JSON通用的方法大致如下:

NSObject* jsonResult = [serializedJSON allValues]; 
if ([jsonResult isKindOfClass:[NSArray class]]) { 
    <handle NSArray> 
} 
else if ([jsonResult isKindOfClass:[NSDictionary class]]) { 
    <handle NSDictionary> 
} 
else if ([jsonResult isKindOfClass:[NSNumber class]]) { 
    <handle NSNumber> 
} 
else if ([jsonResult isKindOfClass:[NSString class]]) { 
    <handle NSString> 
} 
else if (jsonResult == [NSNull null]) { 
    <handle null> 
} 
+0

谢谢!你的救星!我知道必须有某种类型的方法来检查课程,但是当我搜索时我找不到任何方法。再次感谢你! –

+0

@MichaelKing - 请记住,JSON是递归的 - 每个字典和数组包含可能以相同方式进一步解析的元素。 –

相关问题