简体   繁体   中英

Keeping KEYS in correct ORDER in NSDictionary/NSArray from JSON

I have a JSON array returning from a request.
Data is as such (it comes from external source and cannot be changed from server):

[{"clients":
{"name":"Client name here","telephone":"00000","email":"clientemail@hotmail.com","website":"www.clientname.com"}
}]

I receive the JSON array in this order.

I have the below Objective-C:

//jsonString is the json string inside type NSString.
NSMutableDictionary *tempDict = [jsonString JSONValue];
NSMutableDictionary *clients = [tempDict objectForKey:@"clients"];

for(NSString *key in clients) {
        id value = [tempDict objectForKey:key]; 
            for(NSString *itemKey in key) {
                NSLog(@"ITEM KEY %@: ",itemKey);

            }
}

The order it prints keys is:
telephone
email
name
web site

I need it to print how it is received (eg):
name
telephone
email
web site

I have read that Dictionaries are unsorted by definition so am happy to use an Array instead of a dictionary.

But I have seen SEVERAL threads on StackOverflow regarding possible solution (using keysSortedByValueUsingSelector ):
Getting NSDictionary keys sorted by their respective values
Can i sort the NSDictionary on basis of key in Objective-C?
sort NSDictionary keys by dictionary value into an NSArray
Can i sort the NSDictionary on basis of key in Objective-C?

I have tried them and am getting nowhere. Not sure if it is just my implementation which is wrong.

I tried (attempt 1):

NSArray *orderedKeys = [clients keySortedByValueUsingComparator:^NSComparisonResult(id obj1, id obj2){
    return [obj1 compare:obj2];
}];

for(NSString *key in orderedKeys) {
        id value = [tempDict objectForKey:key]; 
            for(NSString *keyThree in key) {
                NSLog(@"API-KEY-ORDER %@: ",keyThree);

            }
}

Receive error: [__NSArrayM keySortedByValueUsingComparator:]: unrecognized selector sent to instance .

Also tried (attempt 2):

NSArray *sortedKeys = [[clients allKeys] sortedArrayUsingSelector: @selector(compare:)];
NSMutableArray *sortedValues = [NSMutableArray array];
for (NSString *key in sortedKeys){
    [sortedValues addObject: [tempDict objectForKey: key]];
    //[sortedValues addObject: [all objectForKey: key]]; //failed
}

But another error on selector even though clients is a NSMutableDictionary.

[__NSArrayM allKeys]: unrecognized selector sent to instance 0x4b6beb0

I am at the point where I think I am going to iterate over the NSMutableDictionary and manually place them into a new NSMutableArray in the correct order.

Any ideas would be very much appreciated?
OR anybodies code snippet attempt at my problem would be equally appreciated.

Not only is NSDictionary in principle unordered, but so are JSON objects. These JSON objects are identical :

[{"clients":
{"name":"Client name here","telephone":"00000","email":"clientemail@hotmail.com","website":"www.clientname.com"}
}]

[{"clients":
{"website":"www.clientname.com","name":"Client name here","telephone":"00000","email":"clientemail@hotmail.com"}
}]

If the server wants to have ordered keys, it needs to send an array. Of course you can just use the keysSortedByValueUsingComparator: method to get the keys in a sorted order.

First, the exception you're seeing is because the selector name is incorrect. You should use keysSortedByValueUsingComparator . Second, you can't sort the keys, because the order in which you're getting them from the server doesn't seem to be in any particular order. If you MUST have them in that order, then send them from the server with a tag ("index") and then sort by this tag.

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