我正在使用基于 REST 的 Web 服务来获取数据。无论 JSON 文档的结构如何,NSDictionary 都会以相同的方式填充。我希望在 Web 服务返回时保留排序。
这是我的代码:
-(void) getData
{
NSURL *url = [NSURL URLWithString:@"http://somewebservice"];
__block ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setCompletionBlock:^{
// Use when fetching text data
NSString *responseString = [request responseString];
NSDictionary *resultsDictionary = [responseString objectFromJSONString];
[jokesArray release];
jokesArray = [resultsDictionary allValues]; //always has the same order.
[jokesArray retain];
[self.tableView reloadData];
// Use when fetching binary data
// NSData *responseData = [request responseData];
}];
[request setFailedBlock:^{
NSError *error = [request error];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"An error occured"
message:[error description]
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
[alert release];
}];
[request startAsynchronous];
}