OStack程序员社区-中国程序员成长平台

标题: ios - 如何使用具有字典数组的键解析字典 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 15:58
标题: ios - 如何使用具有字典数组的键解析字典

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)fetchedDataNSData *)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"]); 
    }
    }

现在我只需要把它改成 if "isReservable"= true then display "begin"



Best Answer-推荐答案


看起来 myslots 是一个 NSDictionary,而不是一个 NSArray ...要遍历它的所有键,更改

for (NSDictionary *slots in myslots) {
    // code
}

for (NSString *slotKey in myslots.allKeys) {
    NSDictionary *slot = [myslots valueForKey:slotKey];
    // code
}

关于ios - 如何使用具有字典数组的键解析字典,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12204006/






欢迎光临 OStack程序员社区-中国程序员成长平台 (http://ostack.cn/) Powered by Discuz! X3.4