Iphone 计算JSON项的xcode
我需要计算这个JSON响应中的项目(post)数量Iphone 计算JSON项的xcode,iphone,ios,xcode,json,Iphone,Ios,Xcode,Json,我需要计算这个JSON响应中的项目(post)数量 2012-06-04 14:09:57.872 horitable[72261:11903] JSON : { posts = ( { post = { eventdate = "2012-03-31"; eventid = 2; eventimage = "http://hernandoz.local
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个事件(post),这是我正在使用的代码
[[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
谁能帮帮我吗。谢谢在您的情况下,您可以这样做
[[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);
您需要首先将json分解为
NSDictionary * dict = [JSON JSONValue];
或
然后
试试这个
NSLog(@"Response members= %@",responseString);
NSArray *array = [(NSDictionary*)[responseString JSONValue] objectForKey:@"posts"];
NSLog(@"Count value= %d",[array count]);
问题是JSON作为一个ID,这是可行的
jsonResponse=[(NSDictionary*)JSON objectForKey:@“posts”]代码>我认为你把“post”改为“post”是错误的。键“posts”包含一个字典数组,每个字典都有一个键“post”。您所做的是从数组中以行的形式获取所有字典
for (NSDictionary* day in jsonResponse) {
并检查字典中的关键“帖子”。实际上,字典里并没有所谓的“posts”键。我是“波斯特”。“post”的值是NSDictionary而不是数组。所以你不能在那里叫count
。您的问题的解决方案是删除for循环中不必要的API
[[AFNetworkActivityIndicatorManager sharedManager] decrementActivityCount];
jsonResponse = JSON;
int shows = 0;
for (NSDictionary* day in jsonResponse) {
shows += 1;
}
NSLog(@"count : %d",shows);
只是想确保您已正确分配NSDictionaryjsonResponse为NSArray且JSON为IDI获取错误-NSArray没有可见的@interface声明选择器“objectforkey”objectforkey不是objectforkey是的,这是我键入错误的方式,但如果您执行NSLog(@“jsonResponse:%@”,jsonResponse),则在Xcode中是objectforkey;我在JSON responseHi得到了同样的结果,那JSONValue是什么?我没有使用任何外部库,只有AFJSIT是JSon库的函数,用于将JSon数据转换为字典或数组。下载sbjson并使用它。
for (NSDictionary* day in jsonResponse) {
[[AFNetworkActivityIndicatorManager sharedManager] decrementActivityCount];
jsonResponse = JSON;
int shows = 0;
for (NSDictionary* day in jsonResponse) {
shows += 1;
}
NSLog(@"count : %d",shows);