0

I used to have trouble parsing json, before I found a really good tutorial which I could follow. When I have been building my own webservices to parse json from I have followed the same steps, but now when I'm trying to parse twitters json-feed I'm drawing blanks. Here's my code.

NSError* error;
NSDictionary* json = [NSJSONSerialization 
                      JSONObjectWithData:responseData //1

                      options:kNilOptions 
                      error:&error];

NSArray* itemNumber = [json objectForKey:@"posts"]; //2

NSUInteger numObjects = [itemNumber count];


arrayTweets = [[NSMutableArray alloc] init];
arrayTimes = [[NSMutableArray alloc] init];


if (numObjects != 0) {

    int i = 0;
    do
    {
        NSDictionary* loan = [itemNumber objectAtIndex:i];


        NSString* text = [(NSDictionary*)loan objectForKey:@"text"];
        [arrayTweets addObject:text];

        NSString *time = [(NSDictionary*)loan objectForKey:@"created_at"];
        [arrayTweets addObject:time];

        i++;

    } while (i < numObjects);


} else {
    NSLog(@"Nej");
}

[tableTweet performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];

Notice the "NSArray* itemNumber = [json objectForKey:@"posts"];". Since my old json-feed used to look something like this...

{"status":"ok","count":4,"count_total":4,"pages":1,"posts":[{"id":58,"type":

it was working since I had the "posts" before :[, but now with the twitter feed it just starts right into:

[{"created_at":"Mon Aug 06 19:16:42 +0000 2012","id":232555817835048961,"...

And I have no idea what to do. I realize this is stupid, but I don't think I'm going to learn unless someone explains it to me.

Any help appreciated!

Jens Hendar
  • 174
  • 1
  • 2
  • 10

1 Answers1

1

Aah, there is a small problem with your code. The root object of your previous feed used to be a dictionary. You can see this by the { sign at the beginning. Your new feed instantly gives you an array back [. So you don't have to parse your data as a dictionary but as as NSArray. Without further ado, explanation here: https://stackoverflow.com/a/8356919/341358

Community
  • 1
  • 1
polyclick
  • 2,704
  • 4
  • 32
  • 58