如何使用包含字典数组的键解析Dictionary
UPDATE-8/31 / 12-所以现在我需要根据“isReservable”= 1来浏览字典数组,然后显示“开始”。 我只是想获取每个字典的数组内容,我可以从那里(现在)。 以下是我的尝试。 感谢大家的帮助,我每天都在学习。
来自php页面的json
{ "slots": { "2012-08-31 00:00:00 America/Los_Angeles": [ { "isPending": false, "isReservable": true, "isReserved": false, "label": " ", "begin": "2012-08-30 17:00:00 America/Los_Angeles", "end": "2012-08-30 17:30:00 America/Los_Angeles", "span": 1 }, { "isPending": false, "isReservable": true, "isReserved": false, "label": " ", "begin": "2012-08-30 17:30:00 America/Los_Angeles", "end": "2012-08-30 18:00:00 America/Los_Angeles", "span": 1 }, { "isPending": false, "isReservable": false, "isReserved": false, "label": " ", "begin": "2012-08-30 18:00:00 America/Los_Angeles", "end": "2012-08-31 00:00:00 America/Los_Angeles", "span": 1 }, { "isPending": false, "isReservable": false, "isReserved": false, "label": " ", "begin": "2012-08-31 00:00:00 America/Los_Angeles", "end": "2012-08-31 08:00:00 America/Los_Angeles", "span": 1 }, { "isPending": false, "isReservable": true, "isReserved": false, "label": " ", "begin": "2012-08-31 08:00:00 America/Los_Angeles", "end": "2012-08-31 08:30:00 America/Los_Angeles", "span": 1 }, { "isPending": false, "isReservable": true, "isReserved": false, "label": " ", "begin": "2012-08-31 08:30:00 America/Los_Angeles", "end": "2012-08-31 09:00:00 America/Los_Angeles", "span": 1 }, { "isPending": false, "isReservable": true, "isReserved": false, "label": " ", "begin": "2012-08-31 09:00:00 America/Los_Angeles", "end": "2012-08-31 09:30:00 America/Los_Angeles", "span": 1 }, { "isPending": false, "isReservable": true, "isReserved": false, "label": " ", "begin": "2012-08-31 09:30:00 America/Los_Angeles", "end": "2012-08-31 10:00:00 America/Los_Angeles", "span": 1 }, { "isPending": false, "isReservable": false, "isReserved": true, "label": "Thomas Kroom", "begin": "2012-08-31 10:00:00 America/Los_Angeles", "end": "2012-08-31 12:30:00 America/Los_Angeles", "span": 5 }, { "isPending": false, "isReservable": true, "isReserved": false, "label": " ", "begin": "2012-08-31 12:30:00 America/Los_Angeles", "end": "2012-08-31 13:00:00 America/Los_Angeles", "span": 1 }, { "isPending": false, "isReservable": true, "isReserved": false, "label": " ", "begin": "2012-08-31 13:00:00 America/Los_Angeles", "end": "2012-08-31 13:30:00 America/Los_Angeles", "span": 1 }, { "isPending": false, "isReservable": true, "isReserved": false, "label": " ", "begin": "2012-08-31 13:30:00 America/Los_Angeles", "end": "2012-08-31 14:00:00 America/Los_Angeles", "span": 1 }, { "isPending": false, "isReservable": true, "isReserved": false, "label": " ", "begin": "2012-08-31 14:00:00 America/Los_Angeles", "end": "2012-08-31 14:30:00 America/Los_Angeles", "span": 1 }, { "isPending": false, "isReservable": true, "isReserved": false, "label": " ", "begin": "2012-08-31 14:30:00 America/Los_Angeles", "end": "2012-08-31 15:00:00 America/Los_Angeles", "span": 1 }, { "isPending": false, "isReservable": true, "isReserved": false, "label": " ", "begin": "2012-08-31 15:00:00 America/Los_Angeles", "end": "2012-08-31 15:30:00 America/Los_Angeles", "span": 1 }, { "isPending": false, "isReservable": true, "isReserved": false, "label": " ", "begin": "2012-08-31 15:30:00 America/Los_Angeles", "end": "2012-08-31 16:00:00 America/Los_Angeles", "span": 1 }, { "isPending": false, "isReservable": true, "isReserved": false, "label": " ", "begin": "2012-08-31 16:00:00 America/Los_Angeles", "end": "2012-08-31 16:30:00 America/Los_Angeles", "span": 1 }, { "isPending": false, "isReservable": true, "isReserved": false, "label": " ", "begin": "2012-08-31 16:30:00 America/Los_Angeles", "end": "2012-08-31 17:00:00 America/Los_Angeles", "span": 1 } ], "2012-09-01 00:00:00 America/Los_Angeles": [ { "isPending": false, "isReservable": true, "isReserved": false, "label": " ", "begin": "2012-08-31 17:00:00 America/Los_Angeles", "end": "2012-08-31 17:30:00 America/Los_Angeles", "span": 1 }, { "isPending": false, "isReservable": true, "isReserved": false, "label": " ", "begin": "2012-08-31 17:30:00 America/Los_Angeles", "end": "2012-08-31 18:00:00 America/Los_Angeles", "span": 1 }, { "isPending": false, "isReservable": false, "isReserved": false, "label": " ", "begin": "2012-08-31 18:00:00 America/Los_Angeles", "end": "2012-09-01 00:00:00 America/Los_Angeles", "span": 1 }, { "isPending": false, "isReservable": false, "isReserved": false, "label": " ", "begin": "2012-09-01 00:00:00 America/Los_Angeles", "end": "2012-09-01 08:00:00 America/Los_Angeles", "span": 1 }, { "isPending": false, "isReservable": true, "isReserved": false, "label": " ", "begin": "2012-09-01 08:00:00 America/Los_Angeles", "end": "2012-09-01 08:30:00 America/Los_Angeles", "span": 1 }, { "isPending": false, "isReservable": true, "isReserved": false, "label": " ", "begin": "2012-09-01 08:30:00 America/Los_Angeles", "end": "2012-09-01 09:00:00 America/Los_Angeles", "span": 1 }, { "isPending": false, "isReservable": true, "isReserved": false, "label": " ", "begin": "2012-09-01 09:00:00 America/Los_Angeles", "end": "2012-09-01 09:30:00 America/Los_Angeles", "span": 1 }, { "isPending": false, "isReservable": true, "isReserved": false, "label": " ", "begin": "2012-09-01 09:30:00 America/Los_Angeles", "end": "2012-09-01 10:00:00 America/Los_Angeles", "span": 1 }, { "isPending": false, "isReservable": true, "isReserved": false, "label": " ", "begin": "2012-09-01 10:00:00 America/Los_Angeles", "end": "2012-09-01 10:30:00 America/Los_Angeles", "span": 1 }, { "isPending": false, "isReservable": true, "isReserved": false, "label": " ", "begin": "2012-09-01 10:30:00 America/Los_Angeles", "end": "2012-09-01 11:00:00 America/Los_Angeles", "span": 1 }, { "isPending": false, "isReservable": true, "isReserved": false, "label": " ", "begin": "2012-09-01 11:00:00 America/Los_Angeles", "end": "2012-09-01 11:30:00 America/Los_Angeles", "span": 1 }, { "isPending": false, "isReservable": true, "isReserved": false, "label": " ", "begin": "2012-09-01 11:30:00 America/Los_Angeles", "end": "2012-09-01 12:00:00 America/Los_Angeles", "span": 1 }, { "isPending": false, "isReservable": true, "isReserved": false, "label": " ", "begin": "2012-09-01 12:00:00 America/Los_Angeles", "end": "2012-09-01 12:30:00 America/Los_Angeles", "span": 1 }, { "isPending": false, "isReservable": true, "isReserved": false, "label": " ", "begin": "2012-09-01 12:30:00 America/Los_Angeles", "end": "2012-09-01 13:00:00 America/Los_Angeles", "span": 1 }, { "isPending": false, "isReservable": true, "isReserved": false, "label": " ", "begin": "2012-09-01 13:00:00 America/Los_Angeles", "end": "2012-09-01 13:30:00 America/Los_Angeles", "span": 1 }, { "isPending": false, "isReservable": true, "isReserved": false, "label": " ", "begin": "2012-09-01 13:30:00 America/Los_Angeles", "end": "2012-09-01 14:00:00 America/Los_Angeles", "span": 1 }, { "isPending": false, "isReservable": true, "isReserved": false, "label": " ", "begin": "2012-09-01 14:00:00 America/Los_Angeles", "end": "2012-09-01 14:30:00 America/Los_Angeles", "span": 1 }, { "isPending": false, "isReservable": true, "isReserved": false, "label": " ", "begin": "2012-09-01 14:30:00 America/Los_Angeles", "end": "2012-09-01 15:00:00 America/Los_Angeles", "span": 1 }, { "isPending": false, "isReservable": true, "isReserved": false, "label": " ", "begin": "2012-09-01 15:00:00 America/Los_Angeles", "end": "2012-09-01 15:30:00 America/Los_Angeles", "span": 1 }, { "isPending": false, "isReservable": true, "isReserved": false, "label": " ", "begin": "2012-09-01 15:30:00 America/Los_Angeles", "end": "2012-09-01 16:00:00 America/Los_Angeles", "span": 1 }, { "isPending": false, "isReservable": true, "isReserved": false, "label": " ", "begin": "2012-09-01 16:00:00 America/Los_Angeles", "end": "2012-09-01 16:30:00 America/Los_Angeles", "span": 1 }, { "isPending": false, "isReservable": true, "isReserved": false, "label": " ", "begin": "2012-09-01 16:30:00 America/Los_Angeles", "end": "2012-09-01 17:00:00 America/Los_Angeles", "span": 1 } ] }
这是我到目前为止所拥有的
- (void)fetchedData:(NSData *)responseData { //parse out the json data NSError* error; //NSLog(@"string is %@", responseData); NSDictionary *json = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:&error]; NSDictionary* myslots =[json objectForKey:@"slots"]; NSLog(@"allslots: %@", myslots); //for (NSString *slotKey in myslots.allKeys) { //NSDictionary *slot = [myslots valueForKey:slotKey]; //NSLog(@"These are all the keys: %@", slotKey); NSArray *keys = [myslots allKeys];
更新 – 这是我如何让它工作:
for (NSString *slotKey in myslots.allKeys) { NSDictionary *slot = [myslots valueForKey:slotKey]; NSArray *tests = [myslots objectForKey:slotKey]; for (NSDictionary *myDict in tests){ NSLog(@"%@", [myDict objectForKey:@"begin"]); } }
现在我只需要更改它以说“isReservable”= true然后显示“开始”
它看起来像myslots是一个NSDictionary,而不是NSArray …迭代它的所有键,改变
for (NSDictionary *slots in myslots) { // code }
对于
for (NSString *slotKey in myslots.allKeys) { NSDictionary *slot = [myslots valueForKey:slotKey]; // code }
myslots是一本字典。 似乎第一个键“2012-08-30 00:00:00 America / Los_Angeles”是与您正在寻找的arrays相关联的键。 我建议重新构建数据,使其具有实际的静态键作为访问字典内值的键。