简体   繁体   中英

Extracting an NSDictionary from within another NSDictionary

My application has an NSDictionary containing many other NSDictionary inside it. If I print out this dictionary it reads as follows:

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";
                            }

What is the best possible way for my application to reach the NSDictionary with the name of:

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

without the need of going through each individual dictionary and finding its object for key?

You can use valueForKeyPath for that. It accepts a path, separated by dots. Example:

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

This depends on the representation of dictionary. If the williamhill part is changing, then it does not work, of course.

There is no way to obtain a reference within a map data type (in this case, an NSDictionary ) without traversing it. Think of the simplified version of this problem: You have a linked list with N elements and you wish to reach the N'th element. Because this is a linked list, you'll have to go through all other N-1 nodes in order to obtain the last reference.

An NSDictionary is a hash based data type in which keys and values are stored. In the case you describe, you have no reference to the nested object (an NSDictionary itself) so you must also traverse all of the dictionaries containing it.

Hope this helps point you in the right direction.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM