• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - 如何使用具有字典数组的键解析字典

[复制链接]
菜鸟教程小白 发表于 2022-12-13 15:58:50 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

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/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap