iosobjective-cxcode

NSDictionary splitting data from API


I'm trying to split JSON data I have in a NSDictonary into smaller parts I can use, however I seem to be stuck.

The data I'm receiving from the API I use is current public transport data. This is the data I receive:

{
    Storingen =     {
        Gepland =         {
            Storing =             {
                Advies = "U kunt gebruikmaken van de omreisroute of de bussen reis tussen Alphen a/d Rijn en Gouda met de NS-bus reis vanaf zondag 29 april van Alphen a/d Rijn naar Gouda met de trein via Woerden reis tussen Leiden Centraal en Gouda via Den Haag Centraal";
                Bericht = "
\n        <p>
\n            <b>Wanneer: van zaterdag 28 april tot en met zondag 6 mei</b>
\n            <br/>
\n            <b>Oorzaak: door geplande werkzaamheden</b>
\n            <br/>
\n            <b>Advies: U kunt gebruikmaken van de omreisroute of de bussen</b>
\n            <br/>\treis tussen Alphen a/d Rijn en Gouda met de NS-bus<br/>\treis vanaf zondag 29 april van Alphen a/d Rijn naar Gouda met de trein via Woerden<br/>\treis tussen Leiden Centraal en Gouda via Den Haag Centraal<br/><br/><b>Extra Reistijd: een kwartier tot een half uur</b><br/></p>
\n    ";
                Oorzaak = "door geplande werkzaamheden";
                Periode = "van zaterdag 28 april tot en met zondag 6 mei";
                Traject = "Alphen a/d Rijn-Gouda";
                Vertraging = "een kwartier tot een half uur";
                id = "2012_apn_gd_28apr_6mei";
            };
        };
        Ongepland = "";
    };
}

So far I have this:

-(void)loadJson
{
    dispatch_async(kBgQueue, ^{
        NSData* data = [NSData dataWithContentsOfURL:kJsonURL];
        [self performSelectorOnMainThread:@selector(fetchedData:) withObject:data waitUntilDone:YES];
    });
    
}

- (void)fetchedData:(NSData *)responseData {
    NSError* error;
    NSDictionary *json = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error];
    NSDictionary *storingen = [json objectForKey:@"Storingen"];
    NSDictionary *gepland = [storingen objectForKey:@"Gepland"];
    
    NSLog(@"%@", json);
    
}

Currently the API is only returning one "Gepland", planned, "Storing", malfunction. But there can be more melfunctions at the save time. The API returns this as an array in the "Storing" key. My question is, how can I, let's say, loop through the "Storing" items and use their data to make a table view with just one label and a detail view with all the other information?


Solution

  • Use can use the data from the dictionary as the data source in your table. What I think you will have in return is an NSArray with NSDictionaries. So it should look like this:

    NSArray *storingen = [json objectForKey:@"Storingen"];
    
    for (NSDictionary *storing in storingen ){
         NSString *advies = [storing objectForKey:@"Advies"];
    }
    

    However, the anoying part with JSON and common pasers is that if there only is 1 disruption (Een storing is one disruption), you will get an NSDictionary, otherwise if there are several you will get an NSArray. You can solve that this way:

    - (void)fetchedData:(NSData *)responseData {
        NSError* error;
        NSDictionary *json = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error];
        id storingen = [json objectForKey:@"Storingen"];
        NSDictionary *gepland = [storingen objectForKey:@"Gepland"];
    
        NSMutableArray *result = [[NSMutableArray alloc] init];
    
        if ([storingen isKindOfClass:[NSArray class]]) {
            result = storingen;
        }
        else {
            [result addObject:storingen];
        }
    }
    

    Now, storingen will always be an array and you can use that array to display it in a UITableView e.g.

    Veel success!