Fórum

JSONObject comes out in bad order

thumbnail
Michael Poznecki, modificado 14 Anos atrás.

JSONObject comes out in bad order

Expert Postagens: 301 Data de Entrada: 10/12/08 Postagens Recentes
Hello all,

I have:

JSONObject jsonObject = JSONFactoryUtil.createJSONObject();

jsonObject.put("a", "1");
jsonObject.put("b", "2");
jsonObject.put("c", "3");
jsonObject.put("d", "4");

...



but when I spit out the results I have

[{"d":"4","b":"2","c":"3","a":"1"}]


How can I get this to come out in the right order without sorting it after?
thumbnail
Amos Fong, modificado 14 Anos atrás.

RE: JSONObject comes out in bad order

Liferay Legend Postagens: 2047 Data de Entrada: 07/10/08 Postagens Recentes
From http://groups.google.com/group/android-developers/browse_thread/thread/0afcf8622cfe8905

Your code is incorrect if you're relying on ordering. I'll get to
that, but first some background which may come in handy:

HashMaps are unordered. If you want to preserve order, there's
LinkedHashMap, which retains the order, so when you iterate over the
map, you get the order in which they were inserted. And there's
TreeMap, which will enforce a sorting order for you, independent of
what order they're supplied in.

Unfortunately, that doesn't help you, since the JSONObject
implementation uses HashMap.
But you can iterate over the keys you get back with JSONObject.keys(),
and store the keys in a TreeSet, sorted according to your preferences,
or in an ArrayList, and call Collections.sort() on it, and then
iterate over the result. That's a simple, robust solution, if the
client knows what order the keys should be in.

But that doesn't give you the original order. The options I see there
are:

* A modified version of the JSON code. I don't recommend it,
especially not to deviate from the standard, but if you have no
control over what you're being sent, this may be your only
alternative. Just take the current code, and whenever it does 'new
HashTable()', do 'new LinkedHashTable()'.

* Modify your protocol to pass the desired key order explicitly (as
the value of another key on the object, or on some single larger
object, etc.). Call JSONObject.keys(), and iterate over that. This is
the most robust solution, but requires server-side cooperation, and
increases the payload size a bit.

I may be slightly misremembering, but if I recall correctly,
preservation of the key ordering was NOT part of the Javascript/
ECMAscript standards -- but was so widely implemented and relied on
that in the most recent round they made it part of the standard-to-be.
HOWEVER, JSON is *NOT* Javascript, it is a data interchange language.
From the JSON web site:

"An object is an unordered set of name/value pairs"

So this is not a JSON bug; it is behaving as expected, for
compatibility across a wide range of languages. So you'll either have
to change your protocol, or accommodate your deviation from the
standard.
Simone Cinti, modificado 6 Anos atrás.

RE: JSONObject comes out in bad order

New Member Postagens: 2 Data de Entrada: 13/01/15 Postagens Recentes
Hi. You said it right but, please note that in particular cases Microsoft's WCF APIs relys on the order of a "__type" hint attribute which had to be always the first of the json object structure. I don't know why and accordingly with the JSON specifications this is out of standard.
I'm working on a web application in Liferay 7 which have to call some remote services based on Microsoft's .NET technology ( emoticon emoticon emoticon ) using RESTful approach, and for the 'BUG' explained before I have to find a way using a modified version of the original JSON apis used by Liferay 7, forcing to use the LinkedHashMap instead of the HashMap or similar solutions that allows to keep the order in the json structure.
So it will be great, even for increasing the 'interoperability' between systems in the real-world, if Liferay's will use ordered structures that just keeps the order of insertions in the JSON objects map, even if it's a non-sense according to the JSON standard.
They only just have to mantain the same order of insertions in map...it's not so much!

For more information about that issue, please also see: https://docs.microsoft.com/en-us/dotnet/framework/wcf/feature-details/stand-alone-json-serialization