ios - 如何从NSDictionary获取值?

时间:2013-03-28 18:22:42

标签: ios5 ios6 nsdictionary

我对NSDictionary非常缺乏经验。我有这个数据结构:

{"aps":
  {"alert":null},
   "custom_control":
    {"type":"topic_comment",
     "object":
        {"topic_id":"1","topic_section":"2","plan_id":"3","user_id":"4"}
    }
}

通过这样做,我可以获得“aps”值:

    NSString *aps = [dictionary objectForKey:@"aps"];

但是由于aps包含一个项目数组,我应该使用什么数据结构而不是NSString?我如何获取custom_control的值和“object”

中的值

谢谢!

1 个答案:

答案 0 :(得分:1)

NSDictionary *dict_object = [[dictionary objectForKey:@"custom_control"]objectForKey:@"object"];

这是您词典的精确层次结构

您的NSDictionary(Keys:aps,custom_control)

(i).aps

   (i.i)NSDictionary(key:alert)

(ii).custom_control

   (ii.i)NSDictionary(keys:type,object)

          (ii.i.i)NSString(key:topic_comment)

          (ii.i.ii)NSDictionary(keys:topic_id,topic_section,plan_id,user_id)

请参阅此link