开发者

Issue with SBJsonParser

开发者 https://www.devze.com 2023-04-10 04:53 出处:网络
my iPhone application is receiving from my web service this json string: {\"res\":true,\"users\":[{\"id\":\"79\",\"username\":\"\"},{\"id\":\"81\",\"username\":\"\"},{\"id\":\"83\",\"username\":\"\"}

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开发者_Go百科 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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号