HernandoZ
HernandoZ

Reputation: 742

xcode counting JSON items

I need to Count the number of Items (post) in this JSON response,

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

}

I know there are only 3 events (post), this is the code I am using

 [[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);
    }

I get an error , but I don't understand why .

-[__NSCFString objectForKey:]: unrecognized selector sent to instance 0x76986f0

can someone please help me out . Thanks

Upvotes: 0

Views: 2815

Answers (5)

rakeshNS
rakeshNS

Reputation: 4257

I think you are mistakely put "posts" instead of "post". The key "posts" contain an array of dictionary, and each dictionary has a key "post". What you are doing is you take all dictionaries from array in lines

  for (NSDictionary* day in jsonResponse) {

and check for an key "posts" in dictionary. Really, there is no key called "posts" in dictionary. I is "post". And the value for "post" is a NSDictionary not an array. So you cant call count there. The solution for your issue is remove un-necessary APIs inside for loop

  [[AFNetworkActivityIndicatorManager sharedManager] decrementActivityCount];
  jsonResponse = JSON;

  int shows = 0;
  for (NSDictionary* day in jsonResponse) {
      shows += 1;
  }

  NSLog(@"count : %d",shows);

Upvotes: 0

HernandoZ
HernandoZ

Reputation: 742

The problem was JSON as an ID, this works

jsonResponse = [(NSDictionary*)JSON objectForKey:@"posts"];

Upvotes: 0

Saad
Saad

Reputation: 8947

you need to first fragmentize json as

NSDictionary * dict = [JSON JSONValue];

or

NSDictionary * dict = [JSON JSONFragmentValue];

then

for (NSDictionary* day in dict) {
    shows += [[day objectForKey:@"posts"] count];

    NSLog(@"count : %d",shows);
}

Upvotes: 0

Ravi Kumar Karunanithi
Ravi Kumar Karunanithi

Reputation: 2218

try this

NSLog(@"Response members= %@",responseString);
NSArray *array = [(NSDictionary*)[responseString JSONValue] objectForKey:@"posts"];
NSLog(@"Count value= %d",[array count]);    

Upvotes: 1

iArezki
iArezki

Reputation: 1271

in your case you can do this

 [[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);

Upvotes: 0

Related Questions