我在通过特定的“”获取 json 格式时遇到问题。我可以看到它是一本字典,所以我把字典变成了一个数组是错误的吗?我正在尝试提取“isReserevble=true”的记录,然后根据用户从 UIDatepicker 中选择的时间在表格视图单元格中显示“开始”。json 是通过 NSlog 实现的,但我无法弄清楚这一点。谢谢
看起来我有一堆字典。我还会使用相同的方法吗?
这是我的代码。
- (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);
}
这是我的 NSLog 所有插槽的结果:
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;
}
);
}
好的:这就是我现在得到的
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: (
)