I have the following code to download a JSON file from my server:
- (void) queryAPI:(NSString*)query withCompletion:(void (^) (id json))block{
NSURL *URL = [NSURL URLWithString:@"http://myAPI.example/myAPIJSONdata"];
NSURLRequest *request = [NSURLRequest requestWithURL:URL];
AFHTTPRequestOperation *op = [[AFHTTPRequestOperation alloc] initWithRequest:request];
op.responseSerializer = [AFJSONResponseSerializer serializer];
[op setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
block(responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
block(nil);
}];
[[NSOperationQueue mainQueue] addOperation:op];
}
and the JSON file is like the following example:
{
"dict":{
"first key": [val1, val2, val3],
"second key": [val1, val2, val3],
"third key": [val1, val2, val3],
"fourth key": [val1, val2, val3]
}
}
I need to keep the keys at the same order they come on JSON file, but when I enumerate the returned NSDictionary with [dict allKeys]
, I get the keys disordered, like this:
fourth key
second key
first key
third key
I also tried to use [dict keyEnumerator]
but the result is exactly the same.
Is there any way to keep the keys at the same order they are on JSON file?