SBJsonParser 的问题
我的 iPhone 应用程序正在从我的 Web 服务接收此 json 字符串:
{"res":true,"users":[{"id":"79","username":""},{"id":"81","username":""},{"id":"83","username":""},{"id":"80","username":""},{"id":"82","username":""}]}
我使用以下代码处理它:
SBJsonParser *jsonParser = [[SBJsonParser alloc] init];
NSError *error = nil;
NSDictionary *dictionary = [jsonParser objectWithString:responseString error:&error];
其中 responseString
是通过 JSON 接收的字符串。
现在,如果我检查 [[dictionary valueForKey:@"res"] boolValue]
它是正确的布尔值。 问题在于 [dictionary objectForKey:@"users"]
我不明白它是什么类型的对象。
我也尝试这样做:
NSLog(@"Is of type NSString?: %@", ([[dictionary objectForKey:@"users"] isMemberOfClass:[NSString class]])? @"Yes" : @"No");
NSLog(@"Is of type NSArray?: %@", ([[dictionary objectForKey:@"users"] isMemberOfClass:[NSArray class]])? @"Yes" : @"No");
NSLog(@"Is of type NSMutableArray?: %@", ([[dictionary objectForKey:@"users"] isMemberOfClass:[NSMutableArray class]])? @"Yes" : @"No");
NSLog(@"Is of type NSDictionary?: %@", ([[dictionary objectForKey:@"users"] isMemberOfClass:[NSDictionary class]])? @"Yes" : @"No");
NSLog(@"Is of type NSMutableDictionary?: %@", ([[dictionary objectForKey:@"users"] isMemberOfClass:[NSMutableDictionary class]])? @"Yes" : @"No");
但它总是说否
。
感谢您的帮助。
my iPhone application is receiving from my web service this json string:
{"res":true,"users":[{"id":"79","username":""},{"id":"81","username":""},{"id":"83","username":""},{"id":"80","username":""},{"id":"82","username":""}]}
I'm handling it with the following code:
SBJsonParser *jsonParser = [[SBJsonParser alloc] init];
NSError *error = nil;
NSDictionary *dictionary = [jsonParser objectWithString:responseString error:&error];
where responseString
is the string received with the JSON.
Now if i check for [[dictionary valueForKey:@"res"] boolValue]
it is correctly a boolean.
The problem is with [dictionary objectForKey:@"users"]
I don't understand what kind of object it is.
I try also with this:
NSLog(@"Is of type NSString?: %@", ([[dictionary objectForKey:@"users"] isMemberOfClass:[NSString class]])? @"Yes" : @"No");
NSLog(@"Is of type NSArray?: %@", ([[dictionary objectForKey:@"users"] isMemberOfClass:[NSArray class]])? @"Yes" : @"No");
NSLog(@"Is of type NSMutableArray?: %@", ([[dictionary objectForKey:@"users"] isMemberOfClass:[NSMutableArray class]])? @"Yes" : @"No");
NSLog(@"Is of type NSDictionary?: %@", ([[dictionary objectForKey:@"users"] isMemberOfClass:[NSDictionary class]])? @"Yes" : @"No");
NSLog(@"Is of type NSMutableDictionary?: %@", ([[dictionary objectForKey:@"users"] isMemberOfClass:[NSMutableDictionary class]])? @"Yes" : @"No");
but it always says No
.
Thank you for your help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您应该使用
isKindOfClass:
而不是isMemberOfClass:
因为集合通常在 Cocoa 中实现为类簇。另外,
NSLog(@"%@", NSStringFromClass([[dictionary objectForKey:@"users"] class]))
的编写比单独检查每个可能的类要短得多。You should use
isKindOfClass:
instead ofisMemberOfClass:
because collections are usually implemented as class clusters in Cocoa.Also,
NSLog(@"%@", NSStringFromClass([[dictionary objectForKey:@"users"] class]))
is much shorter to write than checking every possible class individually.