Question

J'essaie d'analyser le JSon suivant (qui a été validé je pense la dernière fois que j'ai vérifié) :

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

Ma tentative utilise ce qui suit :

NSDictionary *myParsedJson = [myRawJson JSONValue];

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

Erreur:

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

Question:Il me semble que la valeur JSon fait de l'objet myParsedJson un NSArray au lieu d'un NSDictionary.

Comment parcourir les objets appelés name_of_object et accéder à chacun des dictionnaires imbriqués ?Est-ce que je m'y prends de la bonne manière ?

Était-ce utile?

La solution

Le premier argument de NSLog doit être une chaîne.Essayez ceci:

NSLog(@"%@", value);

Autres conseils

Ton value n'est pas une chaîne, simplement parce que vous l'avez tapée ainsi.En fonction de la structure que vous avez publiée, vous aurez un tableau comme objet de niveau supérieur.

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

Le %@ la syntaxe dans NSLog provoque le -description Méthode à appeler sur la valeur;cette méthode renvoie une NSString.Cela signifie que vous pourrait faire NSLog([value description]); Mais ce n'est généralement pas une bonne idée.(Quelqu'un pourrait créer une entrée malveillante susceptible de faire planter votre application.)

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top