简体   繁体   中英

iOS : NSJSONSerialization: Wrong code or wrong JSON structure?

I have some trouble with parsing my JSON object for the past 6 hours. In my code, I stripped a JSON response from server (which has been saved as a NSData object) using the following method:

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

And this is the NSMutableDictionary structure I'm able to 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;
}

Then I intend to save the value of "0" and "1" as NSDictionary using the following method but was prompted an error at runtime:

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];
    ....
    ....
}

I can't save the value as a NSDictionary at the variable testPost. The error message I've got is

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

Anyone has any idea what should I do? I need to save the "0" and "1" values as dictionary as I would need items in them to pluck into a table cell later.

Help!!

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM