【问题标题】:A NSDictionary with some repeat keys带有一些重复键的 NSDictionary
【发布时间】:2011-07-01 08:09:32
【问题描述】:

在将JSON 转换为NSDictionary 后,我得到了这样的NSDictionary

content=(
     {}
         )
content=(
     {}
         )
content=(
     {}
         )

我想在每个“内容”字段中获取数据,但我发现无论我做什么(通过使用所有键、objectforkey、keynumerator)Xcode 总是告诉我terminating app due to uncaught exception 'NSInvalidArgumentException'。我认为用重复键创建一个nsdictionary是不合理的,但我需要解决我项目中的问题。

代码:

{
    "adv_name" = "\U767d\U677f";
    page =     (
                {
            content =             (
                                {
                    content = "";
                    type = "\U56fe\U7247";
                },
                                {
                    content = 3;
                    type = "\U6587\U5b57";
                },
                                {
                    content = "";
                    type = "\U89c6\U9891";
                }
            );
            "content_count" = 3;
        },
                {
            content =             (
                                {
                    content = "";
                    type = "\U56fe\U7247";
                },
                                {
                    content = 2;
                    type = "\U6587\U5b57";
                },
                                {
                    content = "";
                    type = "\U89c6\U9891";
                }
            );
            "content_count" = 3;
        },
                {
            content =             (
                                {
                    content = "";
                    type = "\U56fe\U7247";
                },
                                {
                    content = "";
                    type = "\U89c6\U9891";
                },
                                {
                    content = 12;
                    type = "\U6587\U5b57";
                }
            );
            "content_count" = 3;
        }
    );
    "page_count" = 3;
}

我使用objectEnumerator 找到了解决方案。

【问题讨论】:

  • 看起来你的 NSDictionary 不是你想象的那样。您的解析和分配可能有问题。您需要重写您的问题,并包含一些代码。
  • 我需要先查看一些代码才能确定,但​​如果这些是您的日志消息,这看起来像 NSArray 中的 NSDictionary。虽然没有额外的代码,但不能说更多。
  • 那不是 Objective-C 代码。但如果您自己找到了答案,请关闭此问题。
  • 以后,当您想询问异常时,您需要在问题中包含实际的异常消息,复制并粘贴(不要总结)。此外,当您自己找到解决方案时,请将其发布在答案中,而不是将其编辑到您的问题中。

标签: iphone cocoa json nsdictionary


【解决方案1】:

我认为这是对象的“描述”转储。你得到的是一个包含条目“adv_name”、“page”和“page_count”的字典(NSDictionary)。

字典条目“page”包含一个由 3 个字典组成的数组 (NSArray)。

3 个字典中的每一个在“内容”条目中都包含一个由另外 3 个字典组成的数组。

最里面的字典包含条目“content”和“type”。

【讨论】:

    猜你喜欢
    • 2010-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-02
    相关资源
    最近更新 更多