0

I'm returning JSON with a rough structure like the one below, and I'm trying to figure out how I can count how many platforms there are (in this case, three, but could be anything from 1 to 20 or so). I've returned the JSON into an NSDictionary and am using lines such as these to retrieve the data I need:

_firstLabel.text = _gameDetailDictionary[@"results"][@"name"];

In the above case, it'll grab the name from the results section. Since there are multiple platforms, I need to construct a loop to cycle through each name inside the platforms section. Not too sure how to go about that. All help appreciated!

"results":{
    "platforms":[
        {
            "api_detail_url":"http://",
            "site_detail_url":"http://",
            "id":18,
            "name":"First Name"
        },
        {
            "api_detail_url":"http://",
            "site_detail_url":"http://",
            "id":116,
            "name":"Second Name"
        },
        {
            "api_detail_url":"http://",
            "site_detail_url":"http://",
            "id":22,
            "name":"Third Name"
        }
    ],

EDIT: Here's my fetchJSON method:

- (NSDictionary *) fetchJSONDetail: (NSString *) detailGBID {

    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible: YES];

    NSString *preparedDetailURLString = [NSString stringWithFormat:@"http://whatever/format=json", detailGBID];
    NSLog(@"Doing a detailed search for game ID %@", detailGBID);

    NSData *jsonData = [NSData dataWithContentsOfURL: [NSURL URLWithString:preparedDetailURLString]];

    _resultsOfSearch = [[NSDictionary alloc] init];
    if (jsonData) {
        _resultsOfSearch = [NSJSONSerialization JSONObjectWithData: jsonData
                                                           options: NSJSONReadingMutableContainers
                                                             error: nil];
    }

    [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible: NO];

    NSString *results = _resultsOfSearch[@"number_of_page_results"];
    _numberOfSearchResults = [results intValue];

    NSArray *platforms = [_resultsOfSearch valueForKey:@"platforms"];
    int platformsCount = [platforms count];
    NSLog(@"This game has %d platforms!", platformsCount);

    return _resultsOfSearch;

}

Luke
  • 9,512
  • 15
  • 82
  • 146

1 Answers1

2

The "platforms" JSON field is an array, so assuming you've de-serialised the JSON using something like,

NSMutableDictionary *responseJSON = [NSJSONSerialization JSONObjectWithData:resultsData options:NSJSONReadingMutableContainers error:&error];

Then, you can assign platforms to an NSArray,

NSDictionary *results = [responseJSON valueForKey:@"results"];

NSArray *platforms = [results valueForKey:@"platforms"];

...and find the number of platforms via,

int platformsCount = [platforms count];

In your case, where you want to iterate through the platforms, you can use,

for (NSDictionary *platform in platforms)
{
    // do something for each platform
}
Snips
  • 6,575
  • 7
  • 40
  • 64
  • Looks good, but doesn't appear to have worked. I've added my method above, I've added your NSArray and int to it, but the NSLog below returns zero every time. Does it matter where `platforms` is located in the nesting of the JSON, or will it just seek it out wherever it may be? – Luke Nov 06 '12 at 09:45
  • My mistake, I wasn't extracting the @"results" field from the initial dictionary to take it down a level. I've edited my answer. – Snips Nov 06 '12 at 09:55