2

I've been having a devil of a time trying to get my JSON parsed IN ORDER in IOS5. It is coming in order from the server, so I know it's not that. Here is my code:

NSArray *userData = [update JSONValue];
NSLog(@"USERDATA%@", userData);
NSEnumerator *enumerator = [userData objectEnumerator];
id key;
while (key = [enumerator nextObject]) {
    NSDictionary *value = key;
    NSString *comment = [value objectForKey:@"comment"];
    NSLog(@"USERCOMMENT %@", comment);
}

The first NSLog, everything is looking beautiful. The second NSLog is giving me everything out of order. I'm pretty much at my wit's end.

First NSLOG:

USERDATA{
    1 =     {
        comment = "Test 6";
        photoID = 1;
        postedDate = "2 days ago";
        userID = 17;
        userPic = "members/0/image01.png";
        username = kismet;
    };
    2 =     {
        comment = "Test 5";
        photoID = 1;
        postedDate = "2 days ago";
        userID = 17;
        userPic = "members/0/image01.png";
        username = kismet;
    };
    3 =     {
        comment = "Test 4";
        photoID = 1;
        postedDate = "2 days ago";
        userID = 17;
        userPic = "members/0/image01.png";
        username = kismet;
    };
    4 =     {
        comment = "Test 3";
        photoID = 1;
        postedDate = "2 days ago";
        userID = 17;
        userPic = "members/0/image01.png";
        username = kismet;
    };
    5 =     {
        comment = "Test 2";
        photoID = 1;
        postedDate = "2 days ago";
        userID = 17;
        userPic = "members/0/image01.png";
        username = kismet;
    };
    6 =     {
        comment = "Test 1";
        photoID = 1;
        postedDate = "2 days ago";
        userID = 17;
        userPic = "members/0/image01.png";
        username = kismet;
    };
}

Second NSLog:

USERCOMMENT Test 4
USERCOMMENT Test 6
USERCOMMENT Test 1
USERCOMMENT Test 3
USERCOMMENT Test 5
USERCOMMENT Test 2
1
  • 1
    Can you post a sample of your JSON and the resulting logs? Commented Jul 7, 2012 at 17:48

5 Answers 5

2

The problem is that your top-level object is not an NSArray, it's an NSDictionary. If you send back an array, that will work correctly. The alternative is to get the keys of your top-level dictionary and sort them before iteration.

Sign up to request clarification or add additional context in comments.

Comments

0

The entries of a dictionary are unordered. When you iterate through a dictionary, you may get the keys in any order.

1 Comment

But judging by my code, my reasoning is that the NSDictionary value should get only one object of the NSArray key, so the order in theory should be the order of NSArray userData.
0

You can't do it, unless you write your own JSON parser. Any self-respecting JSON library won't guarantee you the order, if it wants to conform to the JSON spec.

From the definition of the JSON object:

an unordered collection of key:value pairs.

1 Comment

You may get your elements in or out of order, my point is that there's no guarantee of any particular ordering. Sometimes you may get lucky and get them in order, but as an engineer, you should not count on that. If you parse the response yourself though, then you can return the items in the original order.
0

This may not relate to you, but I always do my sorting on the server and send back an ordered JSON string.

Comments

0

For those who are still interested in the solution:

NSDictionary *userData = [update JSONValue];
NSArray *keys = [[userData allKeys] sortedArrayUsingSelector:@selector(compare:)];
NSMutableArray *array = [NSMutableArray arrayWithCapacity: [keys count]];

int i = 0;
for (NSString *key in keys) {
    [array addObject: [userData objectForKey: key]];
    i++;
}

for (NSDictionary *myDict in array) {
    NSString *comment = [myDict objectForKey:@"comment"];
    NSLog(@"USERCOMMENT %@", comment);
}

Returns JSON all in order:

USERCOMMENT Test 6
USERCOMMENT Test 5
USERCOMMENT Test 4
USERCOMMENT Test 3
USERCOMMENT Test 2
USERCOMMENT Test 1

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.