Question

I'm trying to parse the following JSon (which was validated I think last time I checked):

{
    "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";
        }
    );
}

My attempt is using the following:

NSDictionary *myParsedJson = [myRawJson JSONValue];

for(id key in myParsedJson) {
    NSString *value = [myParsedJson objectForKey:key];
    NSLog(value);
}

Error:

-[__NSArrayM length]: unrecognized selector sent to instance 0x6bb7b40

Question: It seems to me that JSon value makes myParsedJson object an NSArray instead of a NSDictionary.

How would I iterate through the objects called name_of_object and access each of the nested dictionaries? Am I going about it the right way?

Was it helpful?

Solution

The first argument of NSLog must be a string. Try this:

NSLog(@"%@", value);

OTHER TIPS

Your value is not a string, just because you've typed it so. Based on the structure you posted you will have an array as your top-level object.

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

The %@ syntax in NSLog causes the -description method to be called on value; this method returns an NSString. That means that you could do NSLog([value description]); but this is generally not a good idea. (Someone could craft malicious input that could crash your app.)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top