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" = "<!-- SC_OFF --><div class=\"md\"><p>Can&#39;t really find anyone else having this issue. I am using this ruby library for reddit api wrapper: <a href=\"https://github.com/paradox460/snoo\">https://github.com/paradox460/snoo</a></p>\n\n<p>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&#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.</p>\n</div><!-- SC_ON -->";
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" = "<div class=\"md\"><p>New accounts are severely rate limited on comment speed to prevent spamming. Once you accumulate some karma, that goes down rather quickly. I&#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.</p>\n\n<p>The API returns an error if you post too quickly (Probably <code>RATELIMIT</code> or <code>SUBREDDIT_RATELIMIT</code>; I don&#39;t remember). You can either drop those comments or put them in a queue and retry later, hoping that your queue doesn&#39;t get too long.</p>\n</div>";
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" = "<div class=\"md\"><p>Alright, thanks. Good to know it goes down with karma. So karma is good for something.</p>\n</div>";
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 から任意のオブジェクトを取得できるようにする必要がありますが、この例外がスローされる理由がわかりません。