ios - 从另一个 NSDictionary 中提取一个 NSDictionary

标签 ios iphone objective-c nsdictionary

我的应用程序有一个 NSDictionary,其中包含许多其他 NSDictionary。如果我打印出这本词典,它的内容如下:

oxip =     {
    created = "2014-02-10 14:42:59";
    lastMsgId = "";
    requestTime = "1.6434";
    response =         {
        code = 001;
        debug = "";
        message = success;
        request = getHierarchyByMarketType;
        text = "\n";
        williamhill =             {
            class =                 {
                id = 1;
                maxRepDate = "2014-02-10";
                maxRepTime = "07:31:48";
                name = "UK Football";
                text = "\n";
                type =                     (
                                            {
                        id = 2;
                        lastUpdateDate = "2013-12-26";
                        lastUpdateTime = "13:32:54";
                        market =                             (
                                                            {
                                betTillDate = "2014-02-15";
                                betTillTime = "15:00:00";
                                date = "2014-02-15";
                                id = 140780553;
                                lastUpdateDate = "2014-02-10";
                                lastUpdateTime = "14:09:13";
                                name = "Queen of the South v Dundee - Match Betting";
                                participant =                                     (
                                                                            {
                                        handicap = "";
                                        id = 496658381;
                                        lastUpdateDate = "2014-02-10";
                                        lastUpdateTime = "14:09:13";
                                        name = Dundee;
                                        odds = "11/8";
                                        oddsDecimal = "2.38";
                                        text = "\n\n\n\n\n\n";
                                    },
                                                                            {
                                        handicap = "";
                                        id = 496658380;
                                        lastUpdateDate = "2014-02-10";
                                        lastUpdateTime = "14:09:13";
                                        name = Draw;
                                        odds = "5/2";
                                        oddsDecimal = "3.50";
                                        text = "\n";
                                    },
                                                                            {
                                        handicap = "";
                                        id = 496658379;
                                        lastUpdateDate = "2014-02-10";
                                        lastUpdateTime = "14:09:13";
                                        name = "Queen of the South";
                                        odds = "11/8";
                                        oddsDecimal = "2.38";
                                        text = "\n";
                                    }
                                );
                                text = "\n";
                                time = "15:00:00";
                            }

我的应用程序到达名称为 NSDictionary 的最佳方式是什么:

name = "Queen of the South v Dundee - Match Betting"

不需要遍历每个单独的字典并找到它的键对象?

最佳答案

您可以为此使用 valueForKeyPath。它接受一条由点分隔的路径。示例:

NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"https://dl.dropboxusercontent.com/u/1365846/21680479.json"]]
                                                     options:0
                                                       error:nil];
NSLog(@"%@", [dict valueForKeyPath:@"response.williamhill.class.type.market.name"]);

这取决于字典的表示。如果 williamhill 部分发生变化,那么它当然不起作用。

关于ios - 从另一个 NSDictionary 中提取一个 NSDictionary,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21680479/

相关文章:

ios - Swift + Pubnub 聊天应用程序加载旧消息以进行滚动

ios - CoreData 获取请求返回空数组

ios - 锁定旋转=iphone上的纵向,ipad没有锁定?

ios - 选择 tableview 元素时缓慢推送

iphone - popviewcontroller 没有调用 viewWillappear

ios - 在 objective-c 中创建动态 TableView 的多个异步请求

iphone - 具有 ASPN、应用内购买等的多个应用程序中的应用程序 ID 和捆绑种子 ID

ios - 将 UIViewControllers 放在其他 UIViewControllers 中是不好的做法吗?

ios - 不会为小距离位置更改调用 didUpdateLocations 委托(delegate)方法

objective-c - dispatch_async vs dispatch_sync 执行顺序