0

{ "period" : 5, "externs" : { "lots" : { "start" : 1, "step" : 0, "stop" : 2 } } }

I'm trying to convert the above JSON string to a JSONObject using simple-json. I do it by:

JSONObject obj = new JSONParser().parse(str);

the problem is that the parser somehow change the variables order to:

{ "externs" : { "lots" : { "stop" : 2 , "start" : 1 , "step" : 0 } } , "period" : 5 }

And I want to keep the exactly same order as in the beginning , any hints?

omarloren
  • 133
  • 2
  • 11
  • I failed to google out SimpleJSON. Is it [json-simple](https://code.google.com/p/json-simple/)? – Nikolay Nov 26 '13 at 19:09
  • 1
    See: http://stackoverflow.com/questions/4515676/keep-the-order-of-the-json-keys-during-json-conversion-to-csv – Kevin Bowersox Nov 26 '13 at 19:09
  • Since JSONObject allows you to get() them in any order does it matter? And as @Kevinbowersox points out, JSON is an unordered collection. – mttdbrd Nov 26 '13 at 19:23
  • @Nikolay yes I meant json-simple. And I understand JSON's philosophy but, isn't it weird that if you give the parser a JSON it just mess around with your string **arbitrary**? – omarloren Nov 26 '13 at 20:20
  • JSON is unordered. You do not need to preserve order. – DwB Nov 26 '13 at 20:38
  • 1
    @DwB, that was already mentioned. There are, however, use cases to come up with to retain the order of the json (string). For example, say you want to add some key/values to a json-config file and save it back to disk. It would be annoying if the order of the keys in such a file is messed up after each edit. – Bart Kiers Nov 26 '13 at 20:41
  • That is the problem I was facing, reading a .json with an order and trying to keep that order while saving those values in an .cvs file. – omarloren Nov 26 '13 at 21:27
  • @BartKiers Hi Bart, could you explain how it could be a problem? Since with org.json.JSONObject you can get the keys out in any order using a get(String key) what difference does it make what order they're stored in? I'm trying to wrap my head around how it would be a problem. Let's take your case: you load the file into a string and parse it with a JSON parser (or JSONObject). You then get an iterator with keys() or you just call get() to retrieve the values you need. – mttdbrd Nov 26 '13 at 22:45
  • 1
    @mttdbrd, like I said: the json file could be a config file which you might edit manually. And when editing it manually, you'd like to be able to find the entries in the same place, right? I know I do. – Bart Kiers Nov 27 '13 at 07:28

1 Answers1

3

What you want opposes how JSON objects are specified: an unordered list of key/value pairs.

Glenn Lane
  • 3,892
  • 17
  • 31
  • I didn't mean to break the law, but if I run a hundred times that code, It'll return the same string in the same order, so it has an order, I just want for it to keep the same order as in the root JSON string. – omarloren Nov 26 '13 at 20:05
  • 1
    @omarloren, the fact that a certain implementation (simple-json, in your case) parses it each time in the same order does not mean said implementation does so with a specific goal in mind. Chances are a HashMap is returned, in which case the "order" (which I cannot call it) is based on the hash code of the keys. Also see this Q&A: http://stackoverflow.com/questions/12938442/how-to-parse-from-json-to-map-with-json-simple-and-retain-key-order – Bart Kiers Nov 26 '13 at 20:35
  • @BartKiers Thank you I solve it with the link provided – omarloren Nov 26 '13 at 21:23