Realinstomp
Realinstomp

Reputation: 532

Getting data from API, need to filter results

I'm working with data from a Foursquare API.

I want to get a list of coffee shops, and am getting that back correctly (I'm using RestKit)... but once I get that list, on my end I need to filter out any coffee shop that is a "Starbucks".

So right now I only know how to pull in all coffee shops, but I don't know how to parse that data once I have it before I serve it into the app table view so that there will be no Starbucks coffee shops listed.

Any ideas how I could do that? Let me know if you need any of my code snippets posted that might help.

EDIT

Normal response type from the API would be:

"venue": [{
    "name": "ABC Coffee Shop", {

So I would need to take "name" and filter out any name that was "Starbucks".

Upvotes: 2

Views: 939

Answers (2)

Chris Wagner
Chris Wagner

Reputation: 21013

If FourSquare doesn't let you apply a filter to the request, to filter on the name "Starbucks" then what I would do with this is the following.

I would start by deserializing the response into a JSON Object, which in this case will be a dictionary.

NSError *error = nil;
NSDictionary *responseDict = [[NSJSONSerialization JSONObjectWithData:foursquareResponse options:0 error: &error];
NSArray *starbucks = nil;
if (!error) {
    NSArray *coffeeShops = responseDict[@"venue"];
    starbucks = [coffeeShops filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"name = 'Starbucks'"]];
} else {
    // handle the error
}

NSLog(@"Starbucks: %@", starbucks");

I didn't test this code but I think it should get you on your way.

Upvotes: 3

MCKapur
MCKapur

Reputation: 9157

Looks like JSON to me, you could just use the built in JSON parser, the NSJSONSerialization class. Here is a method I built that takes an NSData JSON parameter, deserializes it and returns a dictionary.

- (NSMutableDictionary *)deserialize: (NSData *)data {

    return [[NSJSONSerialization JSONObjectWithData:data options:kNilOptions error: nil] mutableCopy];
}

I don't know the structure of Foursquare's response inside out, so you might want to NSLog() the returned dictionary to see how you can now reference to it.

Upvotes: 1

Related Questions