2
votes

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":"[email protected]","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.

2
It appears as if clients is an NSArray and you're trying to perform NSDictionary's selectors on it - Eli Ganem
yes I do not undersand why it is thinking this. You can see above clients is an NSMutableDictionary. - Craig Taub
Place a breakpoint after clients is initiated and type po clients in the console. What class does the console show? - Eli Ganem
says <__NSArrayM 0x4b7c590>( ) with all JSON inside brackets. same even when I use NSMutableDictionary *clients = [[NSMutableDictionary alloc] init]; and then clients = [tempDict objectForKey:@"clients"]; - Craig Taub
Then it's an array. Can you please post the complete json you're parsing? The json in your question seems partial, since it's an array. - Eli Ganem

2 Answers

2
votes

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":"[email protected]","website":"www.clientname.com"}
}]

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

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.

1
votes

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.