Pergunta

Im tentando analisar Twitter tendências, mas eu continuo recebendo um erro de interpretação em "as_of". Alguém sabe por que isso está acontecendo?

EDIT:

Aqui está a im código usando

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;
}
Foi útil?

Solução

Eu não sei exatamente o que o seu problema pode ser, mas eu tive um jogo com a API do Twitter e CCJSON e tem um código de exemplo que parece funcionar. Se você cortar e colá-lo no método applicationDidFinishLaunching de um novo projeto e incluir os arquivos CCJSON ele vai funcionar (espero).

Este código terá o json tendências do twitter, a saída do as_of valor e criar uma matriz de tendências.

// 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 isso é útil, comentário, se você tiver alguma dúvida,

Sam

PS Eu costumava neste site para visualizar a resposta JSON - é feita muito mais fácil ver o que está acontecendo - eu simplesmente cortar e colar o JSON de Twitter para ele :)

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top