Strong Like Bull
Strong Like Bull

Reputation: 11317

How to drill down in the following NSDictionary?

I am getting the following NSDictionary for each cell of my UiTableView. I have to be able to drill down and get the first id value in the assets sub structure below.

{
    assets =     (
                {
            "chunk_size" = 262144;
            file =             {
                "is_dirty" = 0;
                "mongo_grid_f_s_file" =                 {
                    file =                     {
                        "_id" =                         {
                            "$id" = 503a58e09b41648f69000001;
                        };
                        chunkSize = 262144;
                        filename = "/private/var/tmp/phpPPZsUf";
                        length = 5849;
                        md5 = 5f2700f4d953ef866fe1d4967f9965fb;
                        name = Image1;
                        uploadDate =                         {
                            sec = 1346001120;
                            usec = 819000;
                        };
                    };
                    gridfs =                     {
                        chunks =                         {
                            w = 1;
                            wtimeout = 10000;
                        };
                        "chunks_name" = "assets.chunks";
                        "files_name" = "assets.files";
                        w = 1;
                        wtimeout = 10000;
                    };
                };
            };
            id = 503a58e09b41648f69000001;
            length = 5849;
            md5 = 5f2700f4d953ef866fe1d4967f9965fb;
            name = Image1;
            "upload_date" = "0.81900000 1346001120";
        },
                {
            "chunk_size" = 262144;
            file =             {
                "is_dirty" = 0;
                "mongo_grid_f_s_file" =                 {
                    file =                     {
                        "_id" =                         {
                            "$id" = 503a58e09b41648f69000004;
                        };
                        chunkSize = 262144;
                        filename = "/private/var/tmp/phphxvI3H";
                        length = 1551;
                        md5 = f193ffe87eb0138608a206dcf72bf704;
                        name = Image2;
                        uploadDate =                         {
                            sec = 1346001120;
                            usec = 841000;
                        };
                    };
                    gridfs =                     {
                        chunks =                         {
                            w = 1;
                            wtimeout = 10000;
                        };
                        "chunks_name" = "assets.chunks";
                        "files_name" = "assets.files";
                        w = 1;
                        wtimeout = 10000;
                    };
                };
            };
            id = 503a58e09b41648f69000004;
            length = 1551;
            md5 = f193ffe87eb0138608a206dcf72bf704;
            name = Image2;
            "upload_date" = "0.84100000 1346001120";
        }
    );
    coordinates =     {
        latitude = "37.78584";
        longitude = "-122.4064";
    };
    "created_at" = "2012-08-26T12:12:00-0500";
    id = 503a58e09b4164cf5a00000a;
    "number_of_cars" = 1;

}

I have tried the following:

 NSDictionary * classifiedAssets = [classified objectForKey:@"assets"];

Then to get each id, I have done:

[classifiedImages objectForKey:@"id"]

However all I am getting is:

(503a58e09b41648f69000001, 503a58e09b41648f69000004)

I was hoping to get back an NSArray of the various IDs and not a parenthesis comma delimited list.

Any tips?

Upvotes: 1

Views: 260

Answers (2)

Chris Trahey
Chris Trahey

Reputation: 18290

I think you may already have what you need; and here's the explanation:

NSDictionary * classifiedAssets = [classified objectForKey:@"assets"];

Is not exactly what you'd expect: you may think it's an NSDictionary (due to your typing of the variable), but it's actually an NSArray (due to the parenthesis in the JSON instead of curly braces). Then this:

[classifiedImages objectForKey:@"id"]
// by which I *assume* you mean:
[classifiedAssets valueForKey:@"id"]

Is actually taking advantage of a nifty feature of NSArrays: calling valueForKey: on an NSArray gives you an NSArray of the results of calling valueForKey: on each member.

I assume you are talking about the parens and commas in an NSLog line. In that context, a comma-delimited set of things surrounded by parenthesis indicates an NSArray (try logging it's class and see)

Upvotes: 2

Alladinian
Alladinian

Reputation: 35646

To get an array of your asset ids, you could do something like this:

// Assuming that 'classified' is the dictionary you have posted
NSArray *ids = [classified valueForKeyPath:@"assets.id"];

Now, what's interesting about valueForKeyPath is that let's you drill down your objects via dot notation while calling valueForKey for every object if encounters an NSArray

Upvotes: 1

Related Questions