从JSON对UITableViewsortingNSDictionary

我无法按顺序sorting我的NSDictionary,已经过了几个星期,我还没有弄明白,所以我希望有人能帮我一把。

NSDictionary数据是从JSON和数据已经从服务器sorting,它显示在JSON中的顺序,但是当它检索并转换为NSDicitionary,顺序是所有错…

这是JSON

{ "categories":{ "unknownCategoryName":[ { "key1":"value1", "key2":"value2" }], "unknownCategoryName1":[ { "key1":"value1", "key2":"value2" }], "unknownCategoryName2":[ { "key1":"value1", "key2":"value2" }], "unknownCategoryName3":[ { "key1":"value1", "key2":"value2" }] } } 

在收到JSON之前,类别数量和名称是不可知的,所以这就是我用来计算和设置tableView和部分

 NSDictionary *results = [jsonString JSONValue]; NSDictionary *all = [results objectForKey:@"categories"]; self.datasource = all; - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return [self.datasource count]; } - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { NSString *title = [[self.datasource allKeys] objectAtIndex:section]; return title; } 

我想要做的就是列出部分和部分标题作为在JSON的顺序….而不是像一个混乱的显示

unknownCategory3
unknownCategory1
unknownCategory
unknownCategory2

“unknownCategory”是产品类别的名称,它们不是按字母顺序排列的,没有任何编号,并且它们已经在JSON中按顺序sorting和显示…

所以,如果你们能帮上忙,那将会很棒。 提前致谢…

键不在NSDictionaries中sorting,所以你必须先sorting键,然后在你的表视图中使用它们。 所以,而不是使用[self.datasource allKeys]来获得您的节标题,首先创build一个有序的数组:sorted = [[self.datasource allKeys] sortedArrayUsingSelector:@selector(caseInsensitiveCompare :)],然后使用该数组获取titles:title = [sorted objectAtIndex:section]。

编辑完成后回答一个问题:

要使用sorting的数组来获得你想要的值到你的表中,我认为这应该是接近的。 你将不得不添加一个属性,NSArray * sorted,到你的.h文件,然后在你的.m文件中(假设你的json的结构如上所述):

  NSDictionary *results = [jsonString JSONValue]; NSDictionary *all = [results objectForKey:@"categories"]; self.datasource = all; self.sorted = [[self.datasource allKeys] sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)]; - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return [self.sorted count]; } - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { NSString *title = [self.sorted objectAtIndex:section]; return title; } - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return [[self.datasource valueForKey:[self.sorted objectAtIndex:section]]count]; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *cellIdentifier = @"Cell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; if (cell == nil) { cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier]; } NSArray *theArray = [self.datasource valueForKey:[self.sorted objectAtIndex:indexPath.section]]; NSString *text1 = [[theArray objectAtIndex:0] valueForKey:@"key1"]; NSString *text2 = [[theArray objectAtIndex:0] valueForKey:@"key2"]; cell.textLabel.text = [NSString stringWithFormat:@"%@\r%@",text1,text2]; cell.textLabel.numberOfLines=0; // this set the number of lines to unlimited return cell; } 

你没有说你想在一个表格行中显示两个不同的值 – 在这个例子中,我将它们连接成一个string,并在两个string之间进行返回。

看起来奇怪的JSON。 你可以像这样使用

 { "categories": [ "unknownCategoryName": [ { "key1": "value1", "key2": "value2" } ], "unknownCategoryName2": [ { "key1": "value1", "key2": "value2" } ] ] } 

如果你喜欢上面的话就很容易分类。

sorting问题中提供的JSON

 -(NSMutableArray *)getAllKeys:(NSDictionary *)jsonDictionary{ NSDictionary *all = [results objectForKey:@"categories"]; NSMutableArray *allKeys = [all allKeys]; NSSortDescriptor *sorter = [[NSSortDescriptor alloc] initWithKey:@"self" ascending:YES]; NSArray *sortDescriptors = [NSArray arrayWithObject: sorter]; [allKeys sortUsingDescriptors:sortDescriptors]; [sorter release]; return allkeys; } -(void)createSortedDictionary{ NSMutableDictionary *dictionaryMutable=[NSMutableDictionary dictionary]; for(NSString *string in allkeys){ [dictionaryMutable setObject:[all objectForKey:string] forKey:string]; } } 

dictionaryMutable将保存已sorting的字典

你写了这个JSON,还是从某个你无法编辑的地方得到它? 它看起来对我来说是畸形的。 类别应该是一个数组,当前数组的位置是没有意义的。 如果JSON看起来像这样:

 { categories:[ {unknownCategoryName: { key1:value1, key2:value2 }}, {unknownCategoryName2: { key1:value1, key2:value2 }} ] } 

然后你可以读取类别到一个NSArray,你的委托方法看起来像这样(并以正确的顺序显示):

 - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView { return [categoriesArray count]; } - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section { NSDictionary *dict = [categoriesArray objectAtIndex:section]; NSString *title = [[dict allKeys] objectAtIndex:0]; return title; } 

我认为保留原始顺序的唯一方法是在从JSON数据中创buildJSONstring之后自行完成初始parsing。 如果JSONstring的结构与发布的结果相同,则可以使用NSScanner来查找引号之间的string,然后只select那些具有[最后一个引号之后添加到数组中的string,这将是您的命令键数组。 然后,您可以使用JSONparsing器创buildNSDictionary,并通过遍历您的有序键并发送valueForKey:来获取值。