如何在iOS中解析json

时间:2015-02-11 03:06:04

标签: ios json dictionary

这是我从API收到的json字符串:

"incentives_and_benefits" = "{\"food\":{\"0\":\"\"},\"commission\":{\"0\":\"\"},\"uniform\":{\"0\":\"\"},\"transport\":{\"0\":\"\"},\"extras\":{\"0\":\"\"}}";

我将它分配给字典。现在我需要获取字典中的值。 我尝试使用此处给出的解决方案:How to get values from dict

到目前为止我所拥有的:

    NSString *strIncentives = [[self.jobDetailDict objectForKey:@"sub_slots"] objectForKey:@"incentives_and_benefits"];
    NSData *jsonData = [strIncentives dataUsingEncoding:NSUTF8StringEncoding];

    NSError *e;
    NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:jsonData options:nil error:&e];
    NSLog(@"DICT: %@", dict);

    NSString *food = [dict objectForKey:@"food"];
    NSLog(@"DICT: %@", food);
    NSString *commission = [dict objectForKey:@"commission"];
    NSLog(@"DICT: %@", commission);
    NSString *uniform = [dict objectForKey:@"uniform"];
    NSLog(@"DICT: %@", uniform);
    NSString *transport = [dict objectForKey:@"transport"];
    NSLog(@"DICT: %@", transport);
    NSString *extras = [dict objectForKey:@"extras"];
    NSLog(@"DICT: %@", extras);

现在我需要检查食物的值是0还是1 ..当食物的值为1时,我需要获取Hello字符串并显示它。 (这只是一个例子)食物:{“1”:“你好”}

    if([food isEqualToString:@""])
    {
        //the string will be placed here
        NSLog(@"FOOD Available")
    }

当我到达if语句时,我收到错误:

[__NSCFDictionary isEqualToString:]: unrecognized selector sent to instance

我将如何解决这个问题? 请提前帮助并谢谢。

1 个答案:

答案 0 :(得分:0)

 if([[dict objectForKey:@"food"]isEqualToString:@“”]){
    NSLog(@"FOOD Available")
 }