我的应用程序中包含许多其他NSDictionary
的{{1}}。如果我打印出这本字典,它的内容如下:
NSDictionary
我的应用程序以{:1>的名称访问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";
}
的最佳方式是什么?
name =“南方女王v邓迪 - 比赛投注”
无需遍历每个单独的字典并找到其对象的对象?
答案 0 :(得分:1)
您可以使用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
部分正在发生变化,那么它当然不起作用。
答案 1 :(得分:0)
无法遍历地图数据类型(在本例中为NSDictionary
)而无法遍历它。想想这个问题的简化版本:你有一个包含N个元素的链表,你希望到达第N个元素。因为这是一个链表,所以你必须遍历所有其他N-1个节点才能获得最后一个参考。
NSDictionary
是基于散列的数据类型,其中存储了键和值。在您描述的情况下,您没有引用嵌套对象(NSDictionary本身),因此您还必须遍历包含它的所有字典。
希望这有助于指明你正确的方向。