我正在尝试检查我的 .json 文件中的值是“0”还是“1”。
我的 .json 文件:
{
"timetable": [
{
"50mhour": "1",
"on": "1"
},
{
"50hour": "2",
"on": "1"
},
{
"50hour": "50min1",
"on": "1"
},
{
"techhour": "4",
"on": "1"
},
]
}
我创建了一个方法来调用这个 URL:
-(void)makeRequests
{
NSURL *url = [NSURL URLWithString:@"http://content.app.ch/api/timetable.json"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
AFJSONRequestOperation *operation = [AFJSONRequestOperation
JSONRequestOperationWithRequest:request
success:^(NSURLRequest *request, NSHTTPURLResponse *response, id responseObject)
{
self.get = [responseObject objectForKey:@"timetable"];
}
failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id responseObject)
{
NSLog(@"Request Failed with Error: %@, %@", error, error.userInfo);
}];
[operation start];
}
当然,我还创建了一个 NSDictionary
NSDictionary *tempDictionary= [self.get objectAtIndex:1];
我不太确定它的objectAtIndex
价值。
检查值是“0”还是“1”的正确方法是什么我知道isEqualToString:
可能可以做到这一点,但我不确定如何。
更新:尝试将此作为以下答案的建议:
NSNumber *obj = [[self.get objectAtIndex:0] objectForKey:@"on"];
int onValue = [obj intValue];
if (onValue == 1) {
NSLog(@"works!");
}
else {
NSLog(@"doesn't work!");
}
代码有效,但 JSON 值为“1”,NSlog 仍显示“不起作用!”。