5

I am trying to parse a JSON which contains a dictionary like this:

{"key1" : "value1", "key2" : "value2", "key3" : "value3", ..., "key_n", "value_n"}.

I want to parse this JSON to a Dictionary in Swift, but the order is very important to keep. I know that Swift does not have any Ordered Dictionary type, but is there any way to overcome this?

Note that the JSON I'm parsing contains hundreds of keys and there's no value I can sort on (asc. / desc.) afterwards to recreate the order.

halfer
  • 19,824
  • 17
  • 99
  • 186
Nikolaj Simonsen
  • 1,650
  • 3
  • 18
  • 34
  • Why do you want to keep this order? How do you want to use this order afterwards? – Puneet Sharma Feb 07 '17 at 07:44
  • The order will determine how information is displayed in my application. Imagine a FAQ page (for example) where the FAQs are fetched from a backend and the order of the questions are important. – Nikolaj Simonsen Feb 07 '17 at 07:46
  • 1
    Have a look at answers of this question: http://stackoverflow.com/questions/30969688/ordered-dictionary-in-swift. I guess an array of tuples is what you need. You just need to parse your JSON to this format. – Puneet Sharma Feb 07 '17 at 07:49
  • 2
    If you need a sorted list then the server should send a JSON array, not a dictionary. – Martin R Feb 07 '17 at 07:50
  • If you control whatever is producing that JSON, change it to an array if order is important. Otherwise you cannot use standard JSON tools to work with that data (they won't preserve order). – Thilo Feb 07 '17 at 07:50
  • 2
    @Puneet Sharma - Thank you very much! The problem with this solution is that when I parse the JSON object I will lose the order... I don't see how I can parse the JSON (Data object) to a "custom type". – Nikolaj Simonsen Feb 07 '17 at 07:53
  • 1
    @NikMos: You won't be able to do that with the standard JSON parsers, you'd have to roll your own (or dig really deep into the internals of the parser to get the raw tokens). Ideally, change to send an array. – Thilo Feb 07 '17 at 07:54
  • @Martin R - That would be the easiest way for me (not all the other clients who happens to support ordered dictionary). But I get your point... Thank you! – Nikolaj Simonsen Feb 07 '17 at 07:55
  • To keep existing clients happy, you could make a different endpoint for the array-version. Should be a thin layer server-side, not too much trouble. – Thilo Feb 07 '17 at 07:57
  • Same question for Objective-C (which could have been useful for you if there was an answer): http://stackoverflow.com/questions/7198412/keeping-dictionary-keys-of-json-object-in-order-in-objective-c – Thilo Feb 07 '17 at 07:58
  • @Thilo You are right... I was hoping that the ordered dictionary was possible, but it seems that it would be very difficult. Maybe I'll try experimenting with it at some point, and I'll share my experience here. – Nikolaj Simonsen Feb 07 '17 at 08:00
  • 1
    Note that both http://json.org and https://tools.ietf.org/html/rfc7159 define a JSON object as an *unordered* collection of name/value pairs. – Martin R Feb 07 '17 at 08:03
  • @Thilo I get that it conflicts with the idea of hashing. I was thinking that maybe I could parse the Data object to my own type, so I get the raw data and then make my own type. Thank you very much for all your input! I appreciate it! – Nikolaj Simonsen Feb 07 '17 at 08:03
  • 3
    @NikMos: Yes, you can. But it's a lot of work, and just digging your hole deeper. Using your custom almost-JSON format reduces interoperability for no good reason. – Thilo Feb 07 '17 at 08:10

0 Answers0