我为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而不是括号逗号分隔列表。
任何提示?
答案 0 :(得分:2)
我想你可能已经拥有了你需要的东西;这是解释:
NSDictionary * classifiedAssets = [classified objectForKey:@"assets"];
不完全是你所期望的:你可能认为它是一个NSDictionary(由于你输入了变量),但它实际上是一个NSArray(由于JSON中的括号而不是大括号)。然后这个:
[classifiedImages objectForKey:@"id"]
// by which I *assume* you mean:
[classifiedAssets valueForKey:@"id"]
实际上正在利用NSArrays的一个漂亮功能:在NSArray上调用valueForKey:
会为您提供一个NSArray,其中包含对每个成员调用valueForKey:
的结果。
我假设你在谈论NSLog系列中的parens和逗号。在该上下文中,用括号包围的以逗号分隔的一组事物表示 NSArray(尝试记录它的class
并查看)
答案 1 :(得分:1)
要获得一系列资产ID,您可以执行以下操作:
// Assuming that 'classified' is the dictionary you have posted
NSArray *ids = [classified valueForKeyPath:@"assets.id"];
现在,valueForKeyPath
的有趣之处在于,如果遇到valueForKey
NSArray
的同时通过点表示法向下钻取对象