【发布时间】:2011-08-30 14:02:58
【问题描述】:
我正在使用 json 从 twitter 网络服务加载我的应用程序中的表, 使用搜索功能时效果很好,
http://search.twitter.com/search.json?q=mobtuts&rpp=5
the type of json response for the first one is: tenga: {"completed_in" = "0.076"; ...
但是当我使用状态功能时,
[NSURL URLWithString:@"http://twitter.com/statuses/user_timeline.json?id=hypercomputing"]];
the type of json response for the second one is: tenga: ( { contributors = "<null>"; coordinates = "<null>"; "created_at" = "Thu Aug 04 23:26:05 +0000 2011";...
json 的结果不同,所以我的应用程序在从 json 导入后不会将第二次调用视为字典,而是将其视为字符串,[因为 "(" ??]
这里是代码
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { responseData = [[NSMutableData data] retain]; tweets = [NSMutableArray array]; NSURLRequest *request = [NSURLRequest requestWithURL: // [NSURL URLWithString:@"http://twitter.com/statuses/user_timeline.json?id=hypercomputing"]]; [NSURL URLWithString:@"http://search.twitter.com/search.json?q=mobtuts&rpp=5"]];// hypercomputing //[NSURL URLWithString:@"http://twitter.com/statuses/public_timeline.json?id=hypercomputing"]]; [[NSURLConnection alloc] initWithRequest:request delegate:self]; return YES; } #pragma mark NSURLConnection delegate methods - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { [responseData setLength:0]; }
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[responseData appendData:data];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
[connection release];
NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
[responseData release];
NSLog(@"string: %@",responseString);
NSDictionary *results = [responseString JSONValue];
NSLog(@"tenga: %@",results);
//NSLog(@"tenga: %@",[results objectForKey:@"("] );
//NSArray * keys = [results allKeys]; //ensa
//NSLog(@"keys: %@",keys); //ensayo
NSArray *allTweets = [results objectForKey:@"results"];
//NSArray *allTweets = [results objectForKey:@"user"];
NSLog(@"user is: %@",allTweets);
//[viewController setTweets:allTweets];
[window addSubview:viewController.view];
[window makeKeyAndVisible];
}
那么我怎样才能确保从 json 调用中接收到字典呢?,
非常感谢!
【问题讨论】:
标签: ios json twitter dictionary