简体   繁体   中英

Sorting NSDictionary from JSON for UITableView

I am having trouble trying to sort my NSDictionary in order, and it's been weeks and I still haven't figured it out, so I wish some one could give me a hand here...

NSDictionary data is from JSON and the data is already sorted from the server and it displayed in order in the JSON, but when it retrieved and converted to NSDicitionary, the order is all wrong...

This is the JSON

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

The category quantity and its name will not be known until the JSON is received, so this is what I am using to get the count and setting up the tableView and the section

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;
 }

And what I want to do is to list out the section and the section title as the order in the JSON.... rather than a chaos display like

unknownCategory3
unknownCategory1
unknownCategory
unknownCategory2

The "unknownCategory" is Product Category name, they are not in alphabet order, without any number, and they are already sorted and display in order in the JSON...

So, it would be great if you guys could help. Thanks in advance...

Keys are not ordered in NSDictionaries, so you have to sort the keys first before using them in your table view. So, instead of using [self.datasource allKeys] to get your section titles, first create a sorted array: sorted = [[self.datasource allKeys] sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)], and then use that array to get the titles: title = [sorted objectAtIndex:section].

After Edit to answer a further question:

To use the sorted array to get the values you want into your table, I think this should be close. You would have to add a property, NSArray *sorted, to your .h file, and then this in your .m (this is assuming the structure of your json is as you posted above):

 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;
    }

You didn't say how you wanted to display two different values in one table row -- in this example, I concatenated them into one string with a return between the two.

Seems a strange JSON. You could have used like this

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

And it is very easy to sort if you did like above.

To sort JSON provided in your question

-(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 will hold the sorted dictionary

Did you write this JSON or are you getting it from somewhere that you can't edit? It looks malformed to me. categories should be an array and the current array placement makes no sense. If the JSON looked like this:

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

Then you could read categories into an NSArray, and your delegate methods would look like this (and display in proper order):

- (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;
 }

I think the only way you can preserve the original order is to do the initial parsing yourself after creating a JSON string from the JSON data. If the structure of the JSON string is as you posted, you could use an NSScanner to find the strings between quote marks, and then select only the ones that have a [ after the final quotation mark to add to an array which will be your ordered array of keys. You can then create the NSDictionary using a JSON parser, and get the values by looping through your ordered keys and sending them valueForKey:.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM