2009-11-29 4 views
1

En essayant d'analyser les tendances de twitter, je continue d'obtenir une erreur d'analyseur à "as_of". quelquun sait pourquoi cela se passe?Essayer d'analyser les tendances de twitter

EDIT:

Voici le code im en utilisant

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; 
} 
+0

Pouvez-vous poster un échantillon de code et de données? –

+0

Utilisez-vous simplement http://search.twitter.com/trends/current.json? Quelle bibliothèque/framework d'analyse JSON utilisez-vous? Publiez le code que vous utilisez. –

+0

J'utilise CCJSON pour analyser les tendances Publié le code ci-dessus – timothy5216

Répondre

1

Je ne suis pas sûr de ce que votre problème pourrait être mais j'ai eu un jeu avec le twitter api et CCJSON et ont obtenu un exemple de code qui semble fonctionner. Si vous coupez et collez-le dans la méthode applicationDidFinishLaunching d'un nouveau projet et que vous incluez les fichiers CCJSON, cela fonctionnera (espérons-le).

Ce code prendra les tendances json de twitter, affichera la valeur as_of et créera un tableau de tendances.

// 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]); 

Espérons que cela est utile, des commentaires si vous avez des questions,

Sam

PS je this site pour visualiser la réponse JSON - il a fait beaucoup plus facile de voir ce qui se passe - Je viens de couper et coller le JSON de twitter dans le :)

+0

Merci! Cela a fonctionné parfaitement! – timothy5216

Questions connexes