问题
UPDATE-8/31/12- So now I would need to go through the Array of Dictionaries based on "isReservable" = 1 and then display the "begin". I am just trying to get the contents of the array of each dictionary and I can go from there (for now). Below is what I have tried. Thanks to everyone for the help I am learning more everyday.
json from php page
{
"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
}
]
}
Here is what I have so far
- (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];
Update- This is how I am getting it to work:
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"]);
}
}
Now I just need to change it to say if "isReservable" = true then display "begin"
回答1:
it looks like myslots is an NSDictionary, not an NSArray ... to iterate through all it's keys, change
for (NSDictionary *slots in myslots) {
// code
}
for
for (NSString *slotKey in myslots.allKeys) {
NSDictionary *slot = [myslots valueForKey:slotKey];
// code
}
回答2:
myslots is a dictionary. it seems that the first key "2012-08-30 00:00:00 America/Los_Angeles" is the one associated with the array you are looking for. I would recommend to restructure your data to have an actual, static key as the key to access the values inside the dictionary.
来源:https://stackoverflow.com/questions/12204006/how-to-parse-dictionary-with-keys-that-have-array-of-dictionaries