【问题标题】:iOS : NSJSONSerialization: Wrong code or wrong JSON structure?iOS:NSJSONSerialization:错误的代码或错误的 JSON 结构?
【发布时间】:2015-01-14 11:44:05
【问题描述】:

在过去 6 小时内,我在解析我的 JSON 对象时遇到了一些问题。在我的代码中,我使用以下方法从服务器(已保存为 NSData 对象)中剥离了 JSON 响应:

NSMutableDictionary * responseDataRaw = [NSJSONSerialization JSONObjectWithData:serverResponse options: kNilOptions error:nil];

这是我能够 NSlog 的 NSMutableDictionary 结构:

{
Content =     (
            {
        0 =             (
                            {
                DatePosted = "September 23, 2014 at 09:10pm";
                ID = 64;
                MemberID = "Ken Ang";
                Message = "I won't be left out :D";
                PostNumber = 1;
                Status = 1;
                Subject = "Hello from US!";
                TopicID =                     {
                    DatePosted = "September 23, 2014";
                    Enabled = "<span class='label' style='background-color:#17A647; color:#fff'>Yes</span>";
                    ForumID = 1;
                    FriendlyURL = "19-";
                    ID = 19;
                    MemberID = 1;
                    Title = "Hello from US!";
                    ViewCount = 288;
                };
                UserID = 1;
                Username = kenang;
            }
        );
        1 =             (
                            {
                DatePosted = "December 17, 2014 at 03:27pm";
                ID = 66;
                MemberID = "Nobody know";
                Message = "Hai from Chech";
                PostNumber = 2;
                Status = 1;
                Subject = "Re: Hello from US!";
                TopicID =                     {
                    DatePosted = "September 23, 2014";
                    Enabled = "<span class='label' style='background-color:#17A647; color:#fff'>Yes</span>";
                    ForumID = 1;
                    FriendlyURL = "19-";
                    ID = 19;
                    MemberID = 1;
                    Title = "Hello from US!";
                    ViewCount = 288;
                };
                UserID = 15;
                Username = admin;
            }
        );
        Forum = "Member Introduction";
        ForumID = 1;
        ForumTopicTitle = "Hello from US!";
    }
);
Count = 2;
}

然后我打算用下面的方法将“0”和“1”的值保存为NSDictionary,但是在运行时提示错误:

NSArray * responseData = [responseDataRaw objectForKey:@"Content"];

for (id arrayItems in responseData)
{
    NSMutableArray * validKeys = [NSMutableArray array];

    //Enumerate and collect the valid keys: only "0" and "1". So will do a numeric test.
    for (id itemsInside in [arrayItems allKeys])
    {
        NSScanner *scanner = [NSScanner scannerWithString:itemsInside];
        BOOL isNumeric = [scanner scanInteger:NULL] && [scanner isAtEnd];

        if (isNumeric)
        {
            [validKeys addObject:itemsInside];

        }

}


//Using the key to parse the info into our readable NSDictionary
for (NSString * individualKey in validKeys)
{
    NSDictionary * testPost = [arrayItems objectForKey:individualKey];
    ....
    ....
}

我无法将值保存为变量 testPost 中的 NSDictionary。我得到的错误信息是

[__NSCFArray objectForKey:]: unrecognized selector sent to instance 0x170465040

有人知道我该怎么做吗?我需要将“0”和“1”值保存为字典,因为稍后我需要将其中的项目提取到表格单元格中。

救命!!

【问题讨论】:

  • 使用这个网站来验证你的 json jsonlint.com
  • @ErasmoOliveira 我已经验证并且它是一个有效的 JSON。
  • 如果不是正确的 json,它就不会成功解析为字典 @ErasmoOliveira
  • 您有两个validKeys 实例吗?在第一个 for 循环中定义的那个不能从第二个 for 循环中访问。
  • 错误消息清楚地告诉您,您将objectForKey 发送到NSArray 实例,但您的代码将其视为NSDictionary - 查看发生在哪一行,然后检查调试器中的对象 - 你会看到你在哪里做出了错误的假设

标签: ios objective-c iphone json nsjsonserialization


【解决方案1】:

错误消息清楚地告诉您,您将objectForKey 发送到NSArray 实例,但您的代码将其视为 NSDictionary - 查看发生在哪一行,然后检查调试器中的对象 - 您将看看你在哪里做了不正确的假设。

【讨论】:

    猜你喜欢
    • 2016-04-20
    • 2015-01-19
    • 2013-08-30
    • 1970-01-01
    • 2016-10-09
    • 2018-01-05
    • 1970-01-01
    • 2021-11-18
    • 2018-08-24
    相关资源
    最近更新 更多