【问题标题】:how to parse json in iOS如何在 iOS 中解析 json
【发布时间】:2015-02-11 03:06:04
【问题描述】:

这是我从 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 字符串并显示它。 (这只是一个例子)food: {"1":"Hello"}

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

当我到达 if 语句时,我得到一个错误:

[__NSCFDictionary isEqualToString:]: unrecognized selector sent to instance

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

【问题讨论】:

标签: ios json dictionary


【解决方案1】:
 if([[dict objectForKey:@"food"]isEqualToString:@“”]){
    NSLog(@"FOOD Available")
 }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-11-23
    • 2012-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多