iOS : NSJSON序列化:错误的代码或错误的 JSON 结构?

iOS : NSJSONSerialization: Wrong code or wrong JSON structure?

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

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

这是 NSMutable字典结构我可以 NSlog:

{
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”值保存为字典,因为稍后我需要将其中的项目提取到 table 单元格中。

求助!!

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