JSONkit排序问题
我正在使用基于 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];
}
I am using a REST based web service to get data. No matter what the structure of the JSON document, the NSDictionary gets populated the same way. I want the sorting preserved as the web service returns.
Here is my code:
-(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];
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
NSDictionary
中的条目没有固有的顺序;根据定义,它们是未排序的。对于allValues
返回的数组也是如此,正如文档明确指出的那样:之后您需要对数组进行排序。如果要保持 JSON 源中的排序顺序相同,则必须手动解析 JSON 数据并从字典中逐个检索值。或者,如果您知道 JSON 数据的排序方式,只需对
allValues
返回的数组应用相同的排序算法即可。The entries in an
NSDictionary
have no inherent order; they are unsorted by definition. The same is true for the array returned byallValues
, as the documentation clearly says:You will need to sort the array afterwards. If you want to keep the same sort order that is in the JSON source, you would have to parse the JSON data manually and retrieve the values from the dictionary one after another. Or, if you know how the JSON data is sorted, just apply the same sorting algorithm to the array returned by
allValues
.您不应该使用 JSON 字典来存储顺序很重要的内容。正如 http://json.org/ 所说
You shouldn't be using JSON dictionary to store things whose order matters. As http://json.org/ says