1

我为我的 UiTableView 的每个单元格获取以下 NSDictionary。我必须能够向下钻取并获得下面资产子结构中的第一个 id 值。

{
    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;

}

我尝试了以下方法:

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

然后为了获取每个 id,我已经完成了:

[classifiedImages objectForKey:@"id"]

然而,我得到的是:

(503a58e09b41648f69000001, 503a58e09b41648f69000004)

我希望取回各种 ID 的 NSArray,而不是括号逗号分隔的列表。

有小费吗?

4

2 回答 2

2

我想你可能已经拥有了你需要的东西;这是解释:

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

不完全是您所期望的:您可能认为它是一个 NSDictionary(由于您键入了变量),但它实际上是一个 NSArray(由于 JSON 中的括号而不是花括号)。然后这个:

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

实际上是在利用 NSArrays 的一个漂亮特性:调用valueForKey:NSArray 会为您提供调用valueForKey:每个成员的结果的 NSArray。

我假设您正在谈论 NSLog 行中的括号和逗号。在这种情况下,用括号括起来的逗号分隔的一组事物表示一个 NSArray(尝试记录它class并查看)

于 2012-08-26T18:21:52.250 回答
1

要获取您的资产 ID 数组,您可以执行以下操作:

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

现在,有趣的valueForKeyPath是,valueForKey如果遇到NSArray

于 2012-08-26T18:23:21.190 回答