Iphone 试图解析推特趋势
我试图解析twitter趋势,但我一直在“as_of”处遇到一个解析错误。有人知道为什么会这样吗 编辑: 这是我使用的代码Iphone 试图解析推特趋势,iphone,objective-c,json,twitter,Iphone,Objective C,Json,Twitter,我试图解析twitter趋势,但我一直在“as_of”处遇到一个解析错误。有人知道为什么会这样吗 编辑: 这是我使用的代码 NSMutableArray *tweets; tweets = [[NSMutableArray alloc] init]; NSURL *url = [NSURL URLWithString:@"http://search.twitter.com/trends/current.json"]; trendsArray = [[NSMutableArray alloc] i
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;
}
NSMutableArray*推文;
tweets=[[NSMutableArray alloc]init];
NSURL*url=[NSURL URLWithString:@”http://search.twitter.com/trends/current.json"];
trendsArray=[[NSMutableArray alloc]initWithArray:[CCJSONParser objectFromJSON:[NSString stringWithContentsOfURL:url编码:4错误:无]];
NSMutableDictionary*dict=[[NSMutableDictionary alloc]init];
对于(int i=0;i<[trendsArray计数];i++){
dict=[[NSMutableDictionary alloc]init];
//[post setObject:[[currentArray objectAtIndex:i]objectForKey:@“查询”];
[dict setObject:[trendsArray objectAtIndex:i]forKey:@“趋势”];
//[dict setObject:[trendsArray objectAtIndex:i]forKey:@“查询”];
//[post-setObject:[trendsArray-objectAtIndex:i]forKey:@“as_of”];
[tweets addObject:dict];
//post=零;
}
我不太确定您的问题可能是什么,但我已经尝试过twitter api和CCJSON,并获得了一些似乎有效的示例代码。如果您将其剪切并粘贴到新项目的applicationdFinishLaunching
方法中,并包含CCJSON文件,它将正常工作(希望如此)
这段代码将从twitter获取趋势json,输出值的as_并创建一个趋势数组
// 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我曾经可视化JSON响应-这使查看发生了什么变得更容易-我只是将JSON从中剪切并粘贴到其中:)我不太确定您的问题可能是什么,但我已经玩过twitter api和CCJSON,并获得了一些似乎有效的示例代码。如果您将其剪切并粘贴到新项目的
applicationdFinishLaunching
方法中,并包含CCJSON文件,它将正常工作(希望如此)
这段代码将从twitter获取趋势json,输出值的as_并创建一个趋势数组
// 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我曾经将JSON响应可视化-这使查看发生了什么变得更加容易-我只是将JSON从中剪切并粘贴到其中:)你能发布一些示例代码和数据吗?你只是在使用吗?您使用的JSON解析库/框架是什么?发布您正在使用的代码。我正在使用CCJSON解析上面发布的代码。您可以发布一些示例代码和数据吗?您正在使用吗?您使用的JSON解析库/框架是什么?发布您正在使用的代码。我正在使用CCJSON解析上面发布的代码