JSONkit排序问题

发布于 2024-12-12 13:55:09 字数 1362 浏览 0 评论 0原文

我正在使用基于 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

泛滥成性 2024-12-19 13:55:09

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 by allValues, as the documentation clearly says:

The order of the values in the array isn’t defined.

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.

一身骄傲 2024-12-19 13:55:09

您不应该使用 JSON 字典来存储顺序很重要的内容。正如 http://json.org/ 所说

对象是一组无序的名称/值对。

You shouldn't be using JSON dictionary to store things whose order matters. As http://json.org/ says

An object is an unordered set of name/value pairs.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文