I am having issues getting the json format by a specific “”. I can see that it is a dictionary so am I turning the dictionary into an array wrong? I am trying to pull the records that “isReserevble=true” and then display the “begin” with just the time in the table view cells based on the user selection from a UIDatepicker.
The json is coming through with NSlog but I am not able to figure this out. Thanks
It looks like I hae an array of dictionaries. Would I still use the same methods?
here is my code.
- (void)viewDidLoad
{
[super viewDidLoad];
dispatch_async(kBgQueue, ^{
NSData* data = [NSData dataWithContentsOfURL:
kLatestKivaLoansURL];
[self performSelectorOnMainThread:@selector(fetchedData:)
withObject:data waitUntilDone:YES];
});
}
- (void)fetchedData:(NSData *)responseData {
//parse out the json data
NSError* error;
NSDictionary* json = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error];
NSArray* myslots =[json objectForKey:@"slots"];
NSLog(@"allslots: %@", myslots);
NSMutableArray *datesArray = [[NSMutableArray alloc] init];
for (NSDictionary *slots in json){
NSLog(@"isReservable = %@",[myslots objectForKey:@"isReservable"]);
if ([[myslots objectForKey:@"isReservable"]isEqualToString:@"1"])
{
NSLog(@"begin = %@",[myslots objectForKey:@"begin"]);
[datesArray addObject:[myslots objectForKey:@"begin"]];
NSLog(@"slots array count = %d",[datesArray count]);
}
}
NSLog(@"This is the begin: %@", datesArray);
}
Here is my result of NSLog all slots:
2012-08-29 11:54:26.531 GBSB[1137:15b03] allslots: {
"2012-08-29 00:00:00 America/Los_Angeles" = (
{
begin = "2012-08-29 00:00:00 America/Los_Angeles";
end = "2012-08-29 08:00:00 America/Los_Angeles";
isPending = 0;
isReservable = 0;
isReserved = 0;
label = " ";
span = 1;
},
{
begin = "2012-08-29 08:00:00 America/Los_Angeles";
end = "2012-08-29 08:15:00 America/Los_Angeles";
isPending = 0;
isReservable = 1;
isReserved = 0;
label = " ";
span = 1;
}
);
}
Ok: Here is now what I am getting
2012-08-30 09:28:30.812 GBSB[835:15b03] its a dictionary
2012-08-30 09:28:30.812 GBSB[835:15b03] isReservable = (null)
2012-08-30 09:28:30.812 GBSB[835:15b03] isReservable = (null)
2012-08-30 09:28:30.812 GBSB[835:15b03] isReservable = (null)
2012-08-30 09:28:30.813 GBSB[835:15b03] This is the begin: (
)
The error message is – again – helpful. Try to interpret it:
That means, you sent the
objectForKey:message to the JSON string object instead of the parsed NSDictionary object, that’s what’s wrong.