我使用TouchJSON将以下内容转换为NSDictionary对象
// sample JSON object
{
data = (
{
companyId = 4779;
companyName = "ABC Corporation";
},
{
companyId = 4806;
companyName = "EFG Corporation";
}
);
dataCount = 2;
success = 1;
}
NSString *src = @"http://mysite/app1/companyList.php";
NSURL *url = [[NSURL alloc] initWithString:src];
NSData *data = [[NSData alloc] initWithContentsOfURL:url];
NSError *error = nil;
NSDictionary *dic = [[CJSONDeserializer deserializer] deserializeAsDictionary:data error:&error];
int dataCount = (int) [dic valueForKey:@"dataCount"];
// This is where I have problem with. The (dataCount == 1) expression never evaluates to true even if value of dataCount from the JSON object equals to 1.
if ( dataCount == 1 ){
// do something
} else {
// do something else
}
我做错了吗?
我很感激任何帮助。
答案 0 :(得分:1)
[dic valueForKey:@"dataCount"]
将是NSNumber
。您需要在其上调用-intValue
以获得int
。
int dataCount = [[dic valueForKey:@"dataCount"] intValue];
if (dataCount == 1) {
...
}