In JSON data order in response string and data order when parsed in NSMutableDictionary through [JSONvalue] is differ ? I want order in response string and response dictionary should be same.
Asked
Active
Viewed 181 times
2 Answers
0
Dictionaries are not sorted based on keys and it may be cumbersome to do so. I usually solve it following way:
NSMutableDictionary *dataDict = [[NSDictionary dictionaryWithContentsOfFile:finalPath] retain];
NSArray *sortedKeys = [[[statesDictionary allKeys] sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)] retain];
use sortedKeys
to access the dataDict to get proper data.

Prashant Rane
- 436
- 4
- 11
0
The order of elements in an NSDictionary is undefined per definition, so there is no way you can iterate over elements and expect them to be in some specific order.
The easies approach is to keep the keys in a separate NSArray. See this answer for more robust solutions.

Community
- 1
- 1

Martin Wickman
- 19,662
- 12
- 82
- 106