Question

My parse cloud code function is setup to return data in the form of JSON, like so:

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

What I want to do is query this JSON array in my Objective-C code, and perform certain actions based on what's being returned, through the use of the if statement on the bottom. For example, where it says:

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

I want to replace result intValue with a statement that says the value of "Number of matches" from the JSON data is equal to 1.

- (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];
                                            }

                                        }
                                    }];
    }
}
Was it helpful?

Solution

From what I understand, the response JSON data that you get, is an array of dictionaries within a dictionary.

To retrieve each of those values, you may use the following steps:

Step 1:

Separate the array of dictionaries from the result dictionary into an NSArray object.

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

Step 2:

Now that you have the resultArray, you can extract the values that you want as follows:
Suppose you want the value of NSNumber object "Number of matches", You know that its the 3rd object in the resultArray, so its index is 2.

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

Now you can use the [numberOfMatches intValue] wherever you want.

Hope this helps! :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top