트위터 트렌드를 구문 분석하려고합니다
-
08-07-2019 - |
문제
나는 트위터 트렌드를 구문 분석하려고하지만 "AS_OF"에서 구문 분석 오류를 계속 받고 있습니다. 왜 이런 일이 일어나고 있는지 아는 사람이 있습니까?
편집하다:
다음은 IM을 사용하는 코드입니다
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;
}
해결책
나는 당신의 문제가 무엇인지 확실하지 않지만 Twitter API 및 CCJSON과 함께 플레이했으며 작동하는 샘플 코드를 가지고 있습니다. 자르고 붙여 넣으면 applicationDidFinishLaunching
새로운 프로젝트의 방법과 CCJSON 파일을 포함합니다.
이 코드는 트위터에서 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]);
이것이 유용하기를 바랍니다. 질문이 있으시면 의견을 말하십시오.
샘
추신 나는 내가 사용했다 이 지역 JSON 응답을 시각화하려면 - 무슨 일이 일어나고 있는지 확인하는 것이 훨씬 쉬워졌습니다. JSON을 자르고 붙여 넣습니다. 트위터 그것에 :)
제휴하지 않습니다 StackOverflow