【问题标题】:How can I access elements in a NSDictionary which has no key?如何访问没有键的 NSDictionary 中的元素?
【发布时间】:2015-09-10 20:32:07
【问题描述】:

我正在做一个 GET 请求来填写一个 NSDictionary。我用传统的 NSJSONSerialization 填充该字典。我可以打印它,它得到我想要的所有值。但是,我无法迭代或访问该 NSDictionary 中的值,我总是得到一个“NSInvalidArgumentException”。

这是我打印字典时得到的:

(
        {
        "children_type" = category;
        code = realestate;
        leaf = 0;
        locales =         {
            "children_type" = category;
            code = "real estate";
        };
        parent = "";
        price = 0;
    }
)

(它更有价值,但我只放了第一个)。 例如,如果我想打印代码(这里是“房地产”),我如何访问这些元素?执行 [dictionary allkeys] 或 [dictionary allvalues] 甚至 [dictionary objectForKey:] 时出现错误

谢谢!

【问题讨论】:

  • 没有值但没有键的字典。您的 json 是一个数组,其中包含 1 个对象,即字典。
  • 你有我如何访问这本词典的例子吗?不知道该怎么做。

标签: ios iphone ipad cocoa-touch nsdictionary


【解决方案1】:

该 JSON 数据中的顶级对象是一个数组,因此您可以按如下方式枚举它:

for (NSDictionary *dict in array) {
    NSLog(@"children_type=%@", dict[@"children_type"]);
    ...
}

【讨论】:

  • 正是我想要的。非常感谢,我会在解决的时候标记它!
猜你喜欢
  • 1970-01-01
  • 2010-09-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-11
相关资源
最近更新 更多