SBJsonParser 的问题

发布于 2024-12-07 08:15:04 字数 1401 浏览 0 评论 0原文

我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

挖个坑埋了你 2024-12-14 08:15:04

您应该使用 isKindOfClass: 而不是 isMemberOfClass: 因为集合通常在 Cocoa 中实现为类簇。

另外,NSLog(@"%@", NSStringFromClass([[dictionary objectForKey:@"users"] class])) 的编写比单独检查每个可能的类要短得多。

You should use isKindOfClass: instead of isMemberOfClass: 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文