0

I am working on an App and I use my JSON RESTful API. Therefor I parse JSON like in the following example:

NSData * apiReturn;
NSError *myError = nil;
NSDictionary *res = [NSJSONSerialization JSONObjectWithData:apiReturn options:NSJSONReadingMutableLeaves error:&myError];

If I now pass the following JSON encoded String via the API to the App:

{"da": {"name":"dienstlicher Anlass", "duty":true, "type":"dropdown", "values":{"gt":{"name":"Gesprächstermin", "recurring":true, "values":{"fa":{"name":"Firma", "duty":true, "type":"text", "multiline":false}, "ar":{"name":"Anrede", "duty":true, "single":true, "type":"checkbox", "values":{"fr":{"name":"Frau"}, "hr":{"name":"Herr"}}}, "nm":{"name":"Gesprächspartner", "duty":true, "type":"text", "multiline":false}, "bm":{"name":"Bemerkung", "duty":false, "type":"text", "multiline":true}}}} },"bm":{"name":"Bemerkung", "values":[], "type":"text"},"bm2":{"name":"Bemerkung", "values":[], "type":"text"}}

(You can parse this a bit prettier here: http://json.parser.online.fr)

The NSDictionary should contain the following Keys: "da", "bm", "bm2" - Sorted this way, because this is the sorting of the JSON String!

But what Objective-C does, is this:

wrong sorting

Why does Objective-C sort the elements, and why so wrong?

I hope you can help! :)

1 Answers1

3

An NSDictionary is not guaranteed to store the keys in any given order.

The dude
  • 7,896
  • 1
  • 25
  • 50