0

JSON を NSDictionary に変換して呼び出すObjectForKey:と、エラーが発生します。-[__NSCFArray objectForKey:]: unrecognized selector sent to instance 0xa9801d0

変換するための私のコード:

NSError *error;

NSData *responseData = [NSData dataWithContentsOfURL:dataURL];

NSDictionary *json = [NSJSONSerialization
                      JSONObjectWithData:responseData

                      options:kNilOptions
                      error:&error];

NSLog(@"%@",json);
//The exeption is hilighted here:
NSArray *objects = [[json objectForKey:@"data"] objectForKey:@"children"];

これはファイルです:

 {
    data =         {
        after = "<null>";
        before = "<null>";
        children =             (
                            {
                data =                     {
                    "approved_by" = "<null>";
                    author = Jonovono;
                    "author_flair_css_class" = "<null>";
                    "author_flair_text" = "<null>";
                    "banned_by" = "<null>";
                    clicked = 0;
                    created = 1371955129;
                    "created_utc" = 1371926329;
                    distinguished = "<null>";
                    domain = "self.redditdev";
                    downs = 1;
                    edited = 0;
                    hidden = 0;
                    id = 1gv8g1;
                    "is_self" = 1;
                    likes = "<null>";
                    "link_flair_css_class" = "<null>";
                    "link_flair_text" = "<null>";
                    media = "<null>";
                    "media_embed" =                         {
                    };
                    name = "t3_1gv8g1";
                    "num_comments" = 2;
                    "num_reports" = "<null>";
                    "over_18" = 0;
                    permalink = "/r/redditdev/comments/1gv8g1/rate_limiting_on_commenting/";
                    saved = 0;
                    score = 4;
                    selftext = "Can't really find anyone else having this issue. I am using this ruby library for reddit api wrapper: https://github.com/paradox460/snoo\n\nIt seems when I comment only the first one goes through, and any others fail. It does not seem to return a message, but I am guessing it's because of the message that only comment so much in so much time. How can I get around that? Or is something else causing it? Thanks.";
                    "selftext_html" = "&lt;!-- SC_OFF --&gt;&lt;div class=\"md\"&gt;&lt;p&gt;Can&amp;#39;t really find anyone else having this issue. I am using this ruby library for reddit api wrapper: &lt;a href=\"https://github.com/paradox460/snoo\"&gt;https://github.com/paradox460/snoo&lt;/a&gt;&lt;/p&gt;\n\n&lt;p&gt;It seems when I comment only the first one goes through, and any others fail. It does not seem to return a message, but I am guessing it&amp;#39;s because of the message that only comment so much in so much time. How can I get around that? Or is something else causing it? Thanks.&lt;/p&gt;\n&lt;/div&gt;&lt;!-- SC_ON --&gt;";
                    subreddit = redditdev;
                    "subreddit_id" = "t5_2qizd";
                    thumbnail = "";
                    title = "Rate limiting on commenting?";
                    ups = 5;
                    url = "http://www.reddit.com/r/redditdev/comments/1gv8g1/rate_limiting_on_commenting/";
                };
                kind = t3;
            }
        );
        modhash = 6dviotq5igca155758e9e858f1d863870f1b5296d9d571d45d;
    };
    kind = Listing;
},
    {
    data =         {
        after = "<null>";
        before = "<null>";
        children =             (
                            {
                data =                     {
                    "approved_by" = "<null>";
                    author = pipeep;
                    "author_flair_css_class" = "<null>";
                    "author_flair_text" = "<null>";
                    "banned_by" = "<null>";
                    body = "New accounts are severely rate limited on comment speed to prevent spamming. Once you accumulate some karma, that goes down rather quickly. I've found that my bot with a few thousand karma is able to post a few times a minute at peek without any problems.\n\nThe API returns an error if you post too quickly (Probably `RATELIMIT` or `SUBREDDIT_RATELIMIT`; I don't remember). You can either drop those comments or put them in a queue and retry later, hoping that your queue doesn't get too long.";
                    "body_html" = "&lt;div class=\"md\"&gt;&lt;p&gt;New accounts are severely rate limited on comment speed to prevent spamming. Once you accumulate some karma, that goes down rather quickly. I&amp;#39;ve found that my bot with a few thousand karma is able to post a few times a minute at peek without any problems.&lt;/p&gt;\n\n&lt;p&gt;The API returns an error if you post too quickly (Probably &lt;code&gt;RATELIMIT&lt;/code&gt; or &lt;code&gt;SUBREDDIT_RATELIMIT&lt;/code&gt;; I don&amp;#39;t remember). You can either drop those comments or put them in a queue and retry later, hoping that your queue doesn&amp;#39;t get too long.&lt;/p&gt;\n&lt;/div&gt;";
                    created = 1371970411;
                    "created_utc" = 1371941611;
                    distinguished = "<null>";
                    downs = 0;
                    edited = 0;
                    gilded = 0;
                    id = cao9eaf;
                    likes = "<null>";
                    "link_id" = "t3_1gv8g1";
                    name = "t1_cao9eaf";
                    "num_reports" = "<null>";
                    "parent_id" = "t3_1gv8g1";
                    replies =                         {
                        data =                             {
                            after = "<null>";
                            before = "<null>";
                            children =                                 (
                                                                    {
                                    data =                                         {
                                        "approved_by" = "<null>";
                                        author = Jonovono;
                                        "author_flair_css_class" = "<null>";
                                        "author_flair_text" = "<null>";
                                        "banned_by" = "<null>";
                                        body = "Alright, thanks. Good to know it goes down with karma. So karma is good for something.";
                                        "body_html" = "&lt;div class=\"md\"&gt;&lt;p&gt;Alright, thanks. Good to know it goes down with karma. So karma is good for something.&lt;/p&gt;\n&lt;/div&gt;";
                                        created = 1372014449;
                                        "created_utc" = 1371985649;
                                        distinguished = "<null>";
                                        downs = 0;
                                        edited = 0;
                                        gilded = 0;
                                        id = caoivdj;
                                        likes = "<null>";
                                        "link_id" = "t3_1gv8g1";
                                        name = "t1_caoivdj";
                                        "num_reports" = "<null>";
                                        "parent_id" = "t1_cao9eaf";
                                        replies = "";
                                        "score_hidden" = 0;
                                        subreddit = redditdev;
                                        "subreddit_id" = "t5_2qizd";
                                        ups = 1;
                                    };
                                    kind = t1;
                                }
                            );
                            modhash = 6dviotq5igca155758e9e858f1d863870f1b5296d9d571d45d;
                        };
                        kind = Listing;
                    };
                    "score_hidden" = 0;
                    subreddit = redditdev;
                    "subreddit_id" = "t5_2qizd";
                    ups = 1;
                };
                kind = t1;
            }
        );
        modhash = 6dviotq5igca155758e9e858f1d863870f1b5296d9d571d45d;
    };
    kind = Listing;
}
)

JSON から任意のオブジェクトを取得できるようにする必要がありますが、この例外がスローされる理由がわかりません。

4

4 に答える 4

3

)あなたが提供したJSON出力の最後に、その孤独が見えますか。(Xcode の実際のログを見ると、コピーして質問に貼り付けるのを忘れていた一致があることがわかります。

実際に持っているのは辞書ではなく配列である JSON オブジェクトであるため、キーの値を取得しようとすると、コンパイラは配列がそのメッセージに応答しないことを伝えています。

これは、JSON オブジェクトをアンパックするのに役立ちます。必要な値を抽出する辞書の配列を反復処理する必要があります。

于 2013-06-25T18:41:10.360 に答える
1

短いコードを追加して@Abizemの回答を完成させます。あなたが初心者であることがわかり(私はそれについて問題はありません)、プログラミングや新しい言語を始めるとき、コードスニペットが本当に役立つことを知っています(GoogleがJSON解析チュートリアルでいっぱいであり、SOがunrecognized selector解析時に問題を抱えている投稿でいっぱいであることを測定しません。)

したがって、それで始まる JSON がある場合は常に(、JSON が配列であることを意味します。JSON が a で始まる場合、JSON{は辞書です。

JSON 配列からオブジェクトを取得するには、それを反復処理する必要があります。JSON に含まれるオブジェクトは、辞書または配列にすることができます。

したがって、配列に辞書しかない場合は、次を使用する必要があります。

for(NSDictionary *contentDictionary in yourJSONObject) {
  //do stuffs with your dictionary 
}

JSON に配列しかない場合は、次を使用する必要があります。

for(NSArray *contentArray in yourJSONObject) {
//do stuffs with your dictionary 
}

JSON に配列と辞書の両方がある場合は、次を使用する必要があります。

for(id unknownType in yourJSONObject) {
   if([unknownType isKindOfClass:[NSDictionary class]]) {
       NSDictionary *dict = (NSDictionary*)unknownType;
   }
   else if ([unknownType isKindOfClass:[NSArray class]]{
       NSArray *array = (NSArray *)unknownType;
   }   
}

JSON が辞書の場合、NSDictionary メソッドを使用して適切な値を取得できます。

  [dictJSON objectForKey:@"myKey"];
于 2013-06-25T21:09:24.277 に答える
0

JSON は辞書ではなく配列です。ディクショナリとしてアクセスする前に、配列から最初の要素を取得します。

于 2013-06-25T18:41:13.237 に答える
0

オブジェクトは、 ( )jsonのようにアクセスしようとしている NSArray です。NSDictionaryobjectForKey:

于 2013-06-25T18:41:21.273 に答える