views:

515

answers:

1

Im trying to parse twitter trends but i keep getting a parser error at "as_of". anyone know why this is happening?

EDIT:

Here is the code im using

NSMutableArray *tweets;
tweets = [[NSMutableArray alloc] init];
NSURL *url = [NSURL URLWithString:@"http://search.twitter.com/trends/current.json"];
trendsArray = [[NSMutableArray alloc] initWithArray:[CCJSONParser objectFromJSON:[NSString stringWithContentsOfURL:url encoding:4 error:nil]]]; 

NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];

for (int i = 0; i < [trendsArray count]; i++) {
    dict = [[NSMutableDictionary alloc] init];
    //[post setObject: [[currentArray objectAtIndex:i] objectForKey:@"query"]];
    [dict setObject:[trendsArray objectAtIndex:i] forKey:@"trends"];
    //[dict setObject:[trendsArray objectAtIndex:i] forKey:@"query"];
    //[post setObject:[trendsArray objectAtIndex:i] forKey:@"as_of"];
    [tweets addObject:dict];
    //post = nil;
}
+1  A: 

Hi,

I'm not exactly sure what your problem could be but I've had a play with the twitter api and CCJSON and have got some sample code that seems to work. If you cut and paste it into the applicationDidFinishLaunching method of a new project and include the CCJSON files it will just work (hopefully).

This code will take the trends json from twitter, output the as_of value and create an array of trends.

// Make an array to hold our trends
NSMutableArray *trends = [[NSMutableArray alloc] initWithCapacity:10];

// Get the response from the server and parse the json
NSURL *url = [NSURL URLWithString:@"http://search.twitter.com/trends/current.json"];
NSString *responseString = [NSString stringWithContentsOfURL:url encoding:4 error:nil];
NSDictionary *trendsObject = (NSDictionary *)[CCJSONParser objectFromJSON:responseString]; 

// Output the as_of value
NSLog(@"%@", [trendsObject objectForKey:@"as_of"]);

// We also have a list of trends (by date it seems, looking at the json)
NSDictionary *trendsList = [trendsObject objectForKey:@"trends"];

// For each date in this list
for (id key in trendsList) {
    // Get the trends on this date
    NSDictionary *trendsForDate = [trendsList objectForKey:key];

    // For each trend in this date, add it to the trends array
    for (NSDictionary *trendObject in trendsForDate) {
        NSString *name = [trendObject objectForKey:@"name"];
        NSString *query = [trendObject objectForKey:@"query"]; 
        [trends addObject:[NSArray arrayWithObjects:name, query, nil]];
    }
}

// At the point, we have an array called 'trends' which contains all the trends and their queries.
// Lets see it . . .
for (NSArray *array in trends)
    NSLog(@"name: '%@' query: '%@'", [array objectAtIndex:0], [array objectAtIndex:1]);

Hope this is useful, comment if you have any questions,

Sam

PS I used this site to visualise the JSON response - it made it much easier to see what is going on - I just cut and paste the JSON from twitter into it :)

deanWombourne
Thanks! this worked perfectly!
timothy5216