2009-11-29 100 views
1

我试图解析推特趋势,但我不断收到解析错误“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; 
} 
+0

您能发布一些示例代码和数据吗? – 2009-11-29 20:05:33

+0

你刚刚使用http://search.twitter.com/trends/current.json?你使用的是什么JSON解析库/框架?发布您正在使用的代码。 – 2009-11-29 20:11:03

+0

我使用CCJSON解析趋势 发表了上面的代码 – timothy5216 2009-12-01 03:20:32

回答

1

我不完全相信你的问题可能是什么代码IM,但我已经与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响应 - 它使人们更容易看到是怎么回事 - 我只是将其中的JSON从twitter剪切并粘贴到其中:)

+0

谢谢!这工作完美! – timothy5216 2009-12-08 16:41:24