1

我使用 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
}

我做错什么了吗?

我会很感激任何帮助。

4

1 回答 1

1

[dic valueForKey:@"dataCount"]将是一个NSNumber. 你需要调用-intValue它来获得一个int.

int dataCount = [[dic valueForKey:@"dataCount"] intValue];
if (dataCount == 1) {
    ... 
}
于 2011-11-15T15:00:33.027 回答