试图解析Twitter趋势

时间:2009-11-29 19:59:56

标签: iphone objective-c json twitter

我试图解析推特趋势,但我一直在“as_of”收到解析器错误。有人知道为什么会这样吗?

编辑:

以下是使用

的代码
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 个答案:

答案 0 :(得分:1)

我不确定你的问题是什么,但是我有一个关于twitter api和CCJSON的游戏,并且有一些似乎有效的示例代码。如果将其剪切并粘贴到新项目的applicationDidFinishLaunching方法中并包含CCJSON文件,它将起作用(希望如此)。

此代码将从twitter中获取趋势json,输出as_of值并创建一系列趋势。

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

希望这很有用,如果您有任何问题请发表评论,

萨姆

PS我使用this site来可视化JSON响应 - 它使得查看正在发生的事情变得更加容易 - 我只是将twitter中的JSON剪切并粘贴到其中:)

相关问题