2009-11-29 59 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