Search code examples
iphonexcodeios5nslognsjsonserialization

NSJSON Loop Trouple


I am getting my data but it seems like the data is posting in NSLog everytime it finds my "is reservable"=1; I think this should only post once and when I display it in a tableview cell would it post more than once? Here is my NSLog:

2012-08-31 11:35:39.682 GBSB[2168:15b03] These are the times avail: (
)
2012-08-31 11:35:39.683 GBSB[2168:15b03] These are the times avail: (
    "2012-08-31 08:00:00 America/Los_Angeles"
)
2012-08-31 11:35:39.683 GBSB[2168:15b03] These are the times avail: (
    "2012-08-31 08:00:00 America/Los_Angeles",
    "2012-08-31 08:15:00 America/Los_Angeles"
)
2012-08-31 11:35:39.683 GBSB[2168:15b03] These are the times avail: (
    "2012-08-31 08:00:00 America/Los_Angeles",
    "2012-08-31 08:15:00 America/Los_Angeles",
    "2012-08-31 08:30:00 America/Los_Angeles"
)
2012-08-31 11:35:39.684 GBSB[2168:15b03] These are the times avail: (
    "2012-08-31 08:00:00 America/Los_Angeles",
    "2012-08-31 08:15:00 America/Los_Angeles",
    "2012-08-31 08:30:00 America/Los_Angeles",
    "2012-08-31 08:45:00 America/Los_Angeles"
)
2012-08-31 11:35:39.684 GBSB[2168:15b03] These are the times avail: (
    "2012-08-31 08:00:00 America/Los_Angeles",
    "2012-08-31 08:15:00 America/Los_Angeles",
    "2012-08-31 08:30:00 America/Los_Angeles",
    "2012-08-31 08:45:00 America/Los_Angeles",
    "2012-08-31 09:00:00 America/Los_Angeles"
)
2012-08-31 11:35:39.684 GBSB[2168:15b03] These are the times avail: (
    "2012-08-31 08:00:00 America/Los_Angeles",
    "2012-08-31 08:15:00 America/Los_Angeles",
    "2012-08-31 08:30:00 America/Los_Angeles",
    "2012-08-31 08:45:00 America/Los_Angeles",
    "2012-08-31 09:00:00 America/Los_Angeles",
    "2012-08-31 09:15:00 America/Los_Angeles"
)

Here is my code I am using.

- (void)fetchedData:(NSData *)responseData {
    //parse out the json data

    NSError* error;

    NSDictionary *json = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:&error];


    NSDictionary* myslots =[json objectForKey:@"slots"];
    for (NSString *slotKey in myslots.allKeys) {
    NSDictionary *slot = [myslots valueForKey:slotKey];
        NSArray *tests = [myslots objectForKey:slotKey];
        NSMutableArray *timesArray = [[NSMutableArray alloc] init];
    for (NSDictionary *myDays in tests){

        if ([[myDays objectForKey:@"isReservable"] isEqualToNumber:[NSNumber numberWithInt:1]])
          [timesArray addObject:[myDays objectForKey:@"begin"]];

       NSLog(@"These are the times avail: %@", timesArray);

    }
    }

Solution

  • It looks like you are forgetting your brackets for your if statement:

    for (NSDictionary *myDays in tests){
        if ([[myDays objectForKey:@"isReservable"] isEqualToNumber:[NSNumber numberWithInt:1]]) {
            [timesArray addObject:[myDays objectForKey:@"begin"]];
            NSLog(@"These are the times avail: %@", timesArray);
        }
    }
    

    Does that fix it? Test it out and let me know.