我需要计算此JSON响应中的Items(post)数量,
2012-06-04 14:09:57.872 horitable[72261:11903] JSON : {
posts = (
{
post = {
eventdate = "2012-03-31";
eventid = 2;
eventimage = "http://hernandoz.local/~hernandoz/kopict/02_31march2012.jpg";
eventinfo = "02 event";
eventname = "xplosion 02";
};
},
{
post = {
eventdate = "2012-07-07";
eventid = 3;
eventimage = "http://hernandoz.local/~hernandoz/kopict/greg_vs_turner.jpg";
eventinfo = "02 event";
eventname = "Xplosion 02";
};
},
{
post = {
eventdate = "2012-04-29";
eventid = 4;
eventimage = "http://hernandoz.local/~hernandoz/kopict/ko_itclub_apr_2012.jpg";
eventinfo = "KO East London Interclub";
eventname = "KO Interclub";
};
}
);
}
我知道只有3个事件(帖子),这是我正在使用的代码
[[AFNetworkActivityIndicatorManager sharedManager] decrementActivityCount];
NSLog(@"JSON : %@", JSON); //get the JSON response
// 6.1 - Load JSON into internal variable
jsonResponse = JSON;
// 6.2 - Get the number of shows (post)
int shows = 0;
for (NSDictionary* day in jsonResponse) {
shows += [[day objectForKey:@"posts"] count];
NSLog(@"count : %d",shows);
}
我收到错误,但我不明白为什么。
-[__NSCFString objectForKey:]: unrecognized selector sent to instance 0x76986f0
有人可以帮帮我吗。感谢
答案 0 :(得分:1)
试试这个
NSLog(@"Response members= %@",responseString);
NSArray *array = [(NSDictionary*)[responseString JSONValue] objectForKey:@"posts"];
NSLog(@"Count value= %d",[array count]);
答案 1 :(得分:0)
在你的情况下,你可以这样做
[[AFNetworkActivityIndicatorManager sharedManager] decrementActivityCount];
NSLog(@"JSON : %@", JSON); //get the JSON response
// 6.1 - Load JSON into internal variable
jsonResponse = JSON;
// 6.2 - Get the number of shows (post)
int shows = [[jsonResponse objectForKey:@"posts"] count];
NSLog(@"count : %d",shows);
答案 2 :(得分:0)
你需要首先将json碎片化为
NSDictionary * dict = [JSON JSONValue];
或
NSDictionary * dict = [JSON JSONFragmentValue];
然后
for (NSDictionary* day in dict) {
shows += [[day objectForKey:@"posts"] count];
NSLog(@"count : %d",shows);
}
答案 3 :(得分:0)
问题是JSON作为ID,这是有效的
jsonResponse = [(NSDictionary*)JSON objectForKey:@"posts"];
答案 4 :(得分:0)
我认为你错误地把“帖子”而不是“帖子”。键“posts”包含一个字典数组,每个字典都有一个键“post”。你正在做的是你从行中的所有字典
for (NSDictionary* day in jsonResponse) {
并检查字典中的关键字“帖子”。实际上,字典中没有称为“帖子”的键。我是“帖子”。而“post”的值是NSDictionary而不是数组。所以你不能在那里打count
。您的问题的解决方案是在for循环中删除不必要的API
[[AFNetworkActivityIndicatorManager sharedManager] decrementActivityCount];
jsonResponse = JSON;
int shows = 0;
for (NSDictionary* day in jsonResponse) {
shows += 1;
}
NSLog(@"count : %d",shows);