1

Common task, converting strings to a JSON format. Yes, easy, plenty of answers on StackOverflow about this.

What is not as easy is to understand for me is why the order I get is different from the order I put the pair objects in the NSDictionary.

That's the code I wrote:

-(NSString*)createJSONstring
{
    //http://www.raywenderlich.com/5492/working-with-json-in-ios-5
    NSDictionary* dictionary = [NSDictionary dictionaryWithObjectsAndKeys:
                                @"<xx3-xxxx>",@"from",
                                @"<xxx-xxx>",@"to",
                                @"<Bla bla bla>",@"message",
                                @"<2000>",@"posixtime", nil];

    NSArray* notifications = [NSArray arrayWithObjects:dictionary, nil];

    NSError *writeError = nil;
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:notifications options:NSJSONWritingPrettyPrinted error:&writeError];
    NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    NSLog(@"JSON Output: %@", jsonString);

    return jsonString;
}

I would expect to get something like this:

 {
    "from" : "<xx3-xxxx>"
    "to" : "<xxx-xxx>",
    "message" : "<Bla bla bla>",
    "posixtime" : "<2000>",
  }

But I get a different order:

 {
    "posixtime" : "<2000>",       <----- INCORRECT
    "to" : "<xxx-xxx>",
    "message" : "<Bla bla bla>",
    "from" : "<xx3-xxxx>"         <----- INCORRECT  
  }

How can I get the output in the same order as I insert it into the arrays?

[NSDictionary dictionaryWithObjectsAndKeys:                  //I want it to be:
                                    @"<xx3-xxxx>",@"from",         //1st
                                    @"<xxx-xxx>",@"to",            //2nd
                                    @"<Bla bla bla>",@"message",   //3rd
                                    @"<2000>",@"posixtime", nil];  //4th
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
mm24
  • 9,280
  • 12
  • 75
  • 170
  • 2
    Typically with a dictionary the order is irrelevant since you'll access the values by key. Can you expand on the use case that requires the keyed items to be in a consistent order? – GnarlyDog Feb 23 '14 at 19:40
  • 1
    The order of key/value pairs in a JSON "object" is not guaranteed to be preserved. – Hot Licks Feb 23 '14 at 21:14
  • 1
    From json.org: "An object is an *unordered* set of name/value pairs." – Hot Licks Feb 23 '14 at 21:28

1 Answers1

3

The key/value pairs in an NSDictionary have no order. You may add them in that order but no order information is maintained inside the dictionary. So, when you create the JSON it comes out in an arbitrary order.

If you need the content in a certain order then you should either not use a dictionary or you should stream the items into the JSON in the order that you want (for example, with SBJson4StreamWriter).

Wain
  • 118,658
  • 15
  • 128
  • 151
  • 1
    Of course, the parser on the other end does not guarantee to maintain the order either. Entries in a JSON "object" should be considered to be unordered. – Hot Licks Feb 23 '14 at 21:17
  • @HotLicks, I agree they should (be considered to be unordered), but sometimes you need to integrate with another party who have assigned meaning to the order - which is always fun... – Wain Feb 23 '14 at 21:20
  • 1
    If the other side is requiring the entries in a certain order it's probably doing a lot of other things wrong as well. – Hot Licks Feb 23 '14 at 21:22