0

我正在尝试解析以下 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 的对象并访问每个嵌套字典?我会以正确的方式去做吗?

4

2 回答 2

2

NSLog 的第一个参数必须是一个字符串。试试这个:

NSLog(@"%@", value);
于 2012-10-03T01:37:50.450 回答
1

value不是一个字符串,只是因为你已经输入了它。根据您发布的结构,您将拥有一个数组作为您的顶级对象。

NSDictionary *myParsedJson = [myRawJson JSONValue];
for(id key in myParsedJson) {
    id value = [myParsedJson objectForKey:key];
    NSLog(@"%@", value);
}

NSLog 中的%@语法导致-description方法在值上被调用;此方法返回一个 NSString。这意味着您可以这样做NSLog([value description]);,但这通常不是一个好主意。(有人可能会制作可能使您的应用程序崩溃的恶意输入。)

于 2012-10-03T09:21:01.323 回答