-2

I've got a JSON as below.

odds: {
0501: {
x: 2.75,
description: "a"
},
0502: {
x: 3.25,
description: "b"
},
0513: {
x: 3.5,
description: "c"
},
0503: {
x: 3.5,
description: "d"
},
0505: {
x: 7.5,
description: "e"
},
0504: {
x: 7.5,
description: "f"
},
0512: {
x: 10,
description: "g"
}
}

This hash comes from HTTP response as I want to show but the thing that I use JSONModel to map it and there is only way to map that NSDictionary. When map this JSON to NSDictionary (as you can guess) this an unordered and sequence of data comes up mixed.

So, how to map this JSON, without broke up its sequence using JSONModel and NSDictionary ?

miletliyusuf
  • 982
  • 1
  • 10
  • 12
  • what happens when you convert this json to NSDictionary ? – Teja Nandamuri Jun 13 '16 at 15:23
  • 2
    Since there is no such thing as an ordered `NSDictionary`, and the order of keys in a JSON dictionary are irrelevant as well, why are you worrying about order here? – rmaddy Jun 13 '16 at 15:30
  • sequence of x values are important and i should show them as they are. – miletliyusuf Jun 13 '16 at 15:32
  • "sequence of x values are important" Then the original JSON is faulty. This should have been an _array_ of dictionaries. Since it isn't, _you_ will have to turn it into an array of dictionaries. – matt Jun 13 '16 at 15:47
  • "This" JSON is not JSON. – vadian Jun 13 '16 at 15:55
  • This is a part of json and I want to sort a part of it. So there is no needed to share all of it. I thought you may understand that easily – miletliyusuf Jun 13 '16 at 16:19
  • *"sequence of x values are important"* - important to what? Certainly not in the JSON. If your code needs them in a certain order then get the list of keys and order the keys as needed. But there is no need to ensure the keys are sorted in the JSON data. – rmaddy Jun 13 '16 at 19:16
  • To be clear, I want a data structure (lets say oddsStructure) and this structure will have objects which are in "odds" key with the same sequence. When I call oddsStructure.allKeys array they should appear with the same sequence "0501","0502","0513","0503",... – miletliyusuf Jun 13 '16 at 19:24

3 Answers3

0

NSDictionary is inherently unordered:

Are keys and values in an NSDictionary ordered?

If you want to preserve the order of key-value entries, you need to use a data structure other than NSDictionary. Any library that passes your data through an NSDictionary cannot preserve the order.

Community
  • 1
  • 1
Paul Cantrell
  • 9,175
  • 2
  • 40
  • 48
  • I wanted to use NSHashTable but JSONModel can not map the object with that. I need a data structure like Hashmap like in java – miletliyusuf Jun 13 '16 at 15:39
  • HashMap in Java is not order-preserving either. (LinkedHashMap is, however.) If JSONModel returns an NSDictionary, then the order is already destroyed. You need to use a different library to get the order. – Paul Cantrell Jun 14 '16 at 01:40
0

Something I've done in this situation is to sort the dictionary keys in a separate array, in addition to the dictionary. Use the ordered key array to determine how to display your dictionary values.

Wyatt
  • 309
  • 2
  • 11
  • I cant sort the dictionary keys because there is no right way to do that. There are x values in json and they should be sorted. If I sort the keys in odds sequence of x values sequence may change. – miletliyusuf Jun 13 '16 at 15:35
  • You're probably going to have to convert your data into another format then. Iterate through the values and convert the json into an object with the x value and description, then place that into an array and sort on the x value. – Wyatt Jun 13 '16 at 15:44
0

Dictionaries can not be sorted, but as your JSON seems to be an array of objects, iterate thru your resulting NSDictionary with for...in and add the elements to a mutable array.

Afterwards sort the resulting array using .sortInPlace by comparing the x-value.

Michael
  • 182
  • 1
  • 9