在if / else语句中查询NSDictionary的值

时间:2014-05-01 19:52:16

标签: ios objective-c json nsdictionary

我的解析云代码功能设置为以JSON的形式返回数据,如下所示:

response.success
({
                "results": [
                  { "Number of top categories": top2.length },
                            { "Top categories": top2 },  
                         { "Number of matches": userCategoriesMatchingTop2.length }, 
         { "User categories that match search": userCategoriesMatchingTop2 }
                ]
});

我想要做的是在我的Objective-C代码中查询这个JSON数组,并通过使用底部的if语句,根据返回的内容执行某些操作。例如,它说:

if ([result intValue] == 1){
                                                    [self performSegueWithIdentifier:@"ShowMatchCenterSegue" sender:self];
                                                }

我想用{J}数据中“匹配数”的值等于1的语句替换result intValue

- (IBAction)nextButton:(id)sender
{
    if (self.itemSearch.text.length > 0) {
        [PFCloud callFunctionInBackground:@"eBayCategorySearch"
                           withParameters:@{@"item": self.itemSearch.text}
                                    block:^(NSString *result, NSError *error) {
                                        NSLog(@"'%@'", result);

                                        NSData *returnedJSONData = result;

                                            NSError *jsonerror = nil;

                                            NSDictionary *categoryData = [NSJSONSerialization
                                                                     JSONObjectWithData:returnedJSONData
                                                                     options:0
                                                                     error:&jsonerror];

                                            if(error) { NSLog(@"JSON was malformed."); }

                                            // validation that it's a dictionary:
                                            if([categoryData isKindOfClass:[NSDictionary class]])
                                            {
                                                NSDictionary *jsonresults = categoryData;
                                                /* proceed with jsonresults */
                                            }
                                            else
                                            {
                                                NSLog(@"JSON dictionary wasn't returned.");
                                            }



                                        if (!error) {

                                            // if 1 match found clear categoryResults and top2 array
                                            if ([result intValue] == 1){
                                                [self performSegueWithIdentifier:@"ShowMatchCenterSegue" sender:self];
                                            }

                                            // if 2 matches found
                                            else if ([result intValue] == 2){
                                                [self performSegueWithIdentifier:@"ShowUserCategoryChooserSegue" sender:self];
                                                //default to selected categories criteria  -> send to matchcenter -> clear categoryResults and top2 array
                                            }

                                            // if no matches found, and 1 top category is returned
                                            else if ([result intValue] == 2) {
                                                [self performSegueWithIdentifier:@"ShowCriteriaSegue" sender:self];
                                            }
                                            // if no matches are found, and 2 top categories are returned
                                            else if ([result intValue] == 2) {
                                                [self performSegueWithIdentifier:@"ShowSearchCategoryChooserSegue" sender:self];
                                            }

                                        }
                                    }];
    }
}

1 个答案:

答案 0 :(得分:1)

根据我的理解,您获得的响应JSON数据是字典中的字典数组。

要检索其中的每个值,您可以使用以下步骤:

第1步:

将结果字典中的字典数组分隔为NSArray对象。

NSArray *resultArray = [resultDictionary objectForKey:@"results"];

第2步:

现在您已拥有resultArray,您可以按如下方式提取所需的值:
假设您想要NSNumber对象"Number of matches"的值, 您知道它是3rd中的resultArray对象,因此其索引为2

NSDictionary *dictionary = [resultArray objectAtIndex:2];
NSNumber *numberOfMatches = [dictionary objectForKey:@"Number of matches"];

现在,您可以在任意位置使用[numberOfMatches intValue]

希望这有帮助! :)