简体   繁体   中英

How to parse NSDictionary sequentially?

I need to parse a json and get the key value pairs in the same sequence as they are present in response.

Currently what i'm doing is

-(instancetype)initWithJson:(NSDictionary *)responseDict {
      if (self = [super init]){
          NSArray *tempArray = [[NSArray alloc] init];

          NSArray *roomSizesForAcArray = [responseDict valueFromDictionaryWithNSNullCheck:@"roomSizesForAc"];
          NSArray *loadChartForInverterArray = [responseDict valueFromDictionaryWithNSNullCheck:@"loadChartForInverter"];
          if(roomSizesForAcArray && roomSizesForAcArray.count>0){
              self.isInverterChart=false;
              tempArray=roomSizesForAcArray;
          }
          else  if(loadChartForInverterArray && loadChartForInverterArray.count>0){
              self.isInverterChart=true;
              tempArray=loadChartForInverterArray;
          }
          self.arrayOfChartSizeObjects=tempArray;

          if(tempArray && tempArray.count>0){

          //Temp array first object is a dictionary which i need to parse sequentially

              self.arrayOfKeys = [[tempArray objectAtIndex:0] allKeys];

          //This array of keys is random every time and not sequential
          }
   }
   return self;
}

I need to someway parse the dictionary [tempArray objectAtIndex:0] maintaining the order of keys in init.

Not clear what you want, you want dictionary while you are already parsing the dictionary. To get dictionary from the JSON use below code.

NSError* error;
NSDictionary* json = [NSJSONSerialization JSONObjectWithData:receiveData options:kNilOptions error:&error];

To get all the keys and then retrieve the details use

NSArray *sortedKeys = [[jsonDict allKeys]

Once you have keys then get the details

NSError *e = nil;
NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData: data options: NSJSONReadingMutableContainers error: &e];

if (!jsonArray) {
  NSLog(@"Error parsing JSON: %@", e);
} else {
   for(NSDictionary *item in jsonArray) {
      NSLog(@"Item: %@", item);
   }
}

Dictionaries are unordered collections. You say "...maintaining the order of keys in init". There is no such thing.

JSON "Objects", the JSON equivalent of a NSDictionaries/Swift Dictionaries, have the same issue.

Some JSON libraries will preserve the order in which key/value pairs are submitted to be sent, but you should not depend on that. The JSON protocol does not guarantee it.

Once you receive JSON data and convert it to an NSDictionary / Dictionary , the order in which the key/value pairs is sent is lost. The only way I know of to preserve the (already unreliable) order of the key/value pairs from the original JSON data stream is to parse the JSON yourself rather than deserializing it using NSJSONSerialization .

If you want your data in a particular order, you should use an array as the container object to send it. You can send an array of key/value pairs if you need to.

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