我在过去6小时内解析我的JSON对象时遇到了一些麻烦。在我的代码中,我使用以下方法从服务器(已将其保存为NSData对象)中剥离了JSON响应:
NSMutableDictionary * responseDataRaw = [NSJSONSerialization JSONObjectWithData:serverResponse options: kNilOptions error:nil];
这是NSMutableDictionary结构,我能够使用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;
}
然后我打算保存&#34; 0&#34;和&#34; 1&#34;作为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
任何人都知道我该怎么办?我需要保存&#34; 0&#34;和&#34; 1&#34;作为字典的值我将需要其中的项目以便稍后进入表格单元格。
帮助!!
答案 0 :(得分:2)
错误消息告诉您,您已将objectForKey
发送到NSArray
实例,但您的代码将其视为NSDictionary - 请查看此行发生的行,然后检查该对象。调试器 - 您将看到您做出错误假设的位置。