我有問題得到特定的「」JSON格式。我可以看到它是一本字典,所以我把字典變成一個數組是錯誤的?我試圖拉取「isReserevble = true」的記錄,然後根據用戶從UIDatepicker中的選擇,在表格視圖單元格中顯示「開始」的時間。 JSON是通過NSlog來通過,但我無法弄清楚這一點。謝謝NSJSON如何解析
它看起來像我hae字典陣列。我仍然會使用相同的方法嗎?
這是我的代碼。
- (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: (
)
是'json'與屬性'json'相關的ivar嗎?你沒有明顯的理由使用兩者。 – Jim
我是新來的,所以我可能犯了一個錯誤 – TIDev