Note that there are some explanatory texts on larger screens.

plurals
  1. POKeeping KEYS in correct ORDER in NSDictionary/NSArray from JSON
    primarykey
    data
    text
    <p>I have a JSON array returning from a request.<br/> Data is as such (it comes from external source and cannot be changed from server):</p> <pre><code>[{"clients": {"name":"Client name here","telephone":"00000","email":"clientemail@hotmail.com","website":"www.clientname.com"} }] </code></pre> <p>I receive the JSON array in this order.</p> <p>I have the below Objective-C:</p> <pre><code>//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); } } </code></pre> <p>The order it prints keys is:<br/> telephone<br/> email<br/> name<br/> web site<br/></p> <p>I need it to print how it is received (eg):<br/> name<br/> telephone<br/> email <br/> web site<br/></p> <p>I have read that Dictionaries are unsorted by definition so am happy to use an Array instead of a dictionary.</p> <p>But I have seen SEVERAL threads on StackOverflow regarding possible solution (using <code>keysSortedByValueUsingSelector</code>):<br/> <a href="https://stackoverflow.com/questions/9708742/getting-nsdictionary-keys-sorted-by-their-respective-values">Getting NSDictionary keys sorted by their respective values</a> <br/> <a href="https://stackoverflow.com/questions/3583020/can-i-sort-the-nsdictionary-on-basis-of-key-in-objective-c">Can i sort the NSDictionary on basis of key in Objective-C?</a><br/> <a href="https://stackoverflow.com/questions/11217421/sort-nsdictionary-keys-by-dictionary-value-into-an-nsarray">sort NSDictionary keys by dictionary value into an NSArray</a><br/> <a href="https://stackoverflow.com/questions/3583020/can-i-sort-the-nsdictionary-on-basis-of-key-in-objective-c?lq=1">Can i sort the NSDictionary on basis of key in Objective-C?</a><br/></p> <p>I have tried them and am getting nowhere. Not sure if it is just my implementation which is wrong.</p> <p>I tried (attempt 1):</p> <pre><code>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); } } </code></pre> <p>Receive error: <code>[__NSArrayM keySortedByValueUsingComparator:]: unrecognized selector sent to instance</code>. <br/></p> <p>Also tried (attempt 2):</p> <pre><code>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 } </code></pre> <p>But another error on selector even though clients is a NSMutableDictionary.</p> <pre><code>[__NSArrayM allKeys]: unrecognized selector sent to instance 0x4b6beb0 </code></pre> <p>I am at the point where I think I am going to iterate over the <code>NSMutableDictionary</code> and manually place them into a new <code>NSMutableArray</code> in the correct order.</p> <p>Any ideas would be very much appreciated?<br/> OR anybodies code snippet attempt at my problem would be equally appreciated.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload