【发布时间】:2012-10-03 00:51:58
【问题描述】:
我正在尝试解析以下 JSon(我认为上次检查时已验证):
{
"top_level" = (
{
"download" = "http:/target.com/some.zip";
"other_information" = "other info";
"notes" = (
{
obj1 = "some text";
obj2 = "more notes";
obj3 = "some more text still";
}
);
title = "name_of_object1";
},
{
"download" = "http:/target.com/some.zip";
"other_information" = "other info";
"notes" = (
{
obj1 = "some text";
obj2 = "more notes";
obj3 = "some more text still";
}
);
title = "name_of_object2";
},
{
"download" = "http:/target.com/some.zip";
"other_information" = "other info";
"notes" = (
{
obj1 = "some text";
obj2 = "more notes";
obj3 = "some more text still";
}
);
title = "name_of_object3";
}
);
}
我的尝试是使用以下内容:
NSDictionary *myParsedJson = [myRawJson JSONValue];
for(id key in myParsedJson) {
NSString *value = [myParsedJson objectForKey:key];
NSLog(value);
}
错误:
-[__NSArrayM length]: unrecognized selector sent to instance 0x6bb7b40
问题: 在我看来,JSon 值使 myParsedJson 对象成为 NSArray 而不是 NSDictionary。
如何遍历名为 name_of_object 的对象并访问每个嵌套字典?我的方法正确吗?
【问题讨论】:
-
Jesse 已经给出了我认为正确的答案,我将做出标准的间接相关评论:由于 iOS 5 Apple 提供了内置的 JSON 解析器;您可能出于充分的理由使用它,但不要期望 SBJSON 解析器在未来得到很好的支持。
-
哦,谢谢汤米;我完全没有意识到这一点。
-
@Tommy,是什么让你这么想?我是 SBJson 的作者,无意停止支持。
-
@StigBrautset 关于使用的简单推测;您现在会看到使用率显着降低,因为您的工作(尽管它很棒)不再具有强大的用例。当项目不再那么重要时,大多数作者会将他们的才能重新投入到一个新项目中。你显然是一个有才华的人,所以我的猜测是你更有可能专注于人们更可能使用的东西。
-
@Tommy 同意了,但它现在已经相当成熟,实际上不需要太多的努力来维护。此外,它还具有 Apple 解析器所没有的功能,因此即使在 iOS 5 及更高版本上它仍然占有一席之地。
标签: iphone objective-c ios ipad sbjson