<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <title>JSONObject comes out in bad order</title>
  <link rel="self" href="https://liferay.dev/nl/c/message_boards/find_thread?p_l_id=119785294&amp;threadId=4681224" />
  <subtitle>JSONObject comes out in bad order</subtitle>
  <id>https://liferay.dev/nl/c/message_boards/find_thread?p_l_id=119785294&amp;threadId=4681224</id>
  <updated>2026-06-30T15:12:12Z</updated>
  <dc:date>2026-06-30T15:12:12Z</dc:date>
  <entry>
    <title>RE: JSONObject comes out in bad order</title>
    <link rel="alternate" href="https://liferay.dev/nl/c/message_boards/find_message?p_l_id=119785294&amp;messageId=96255446" />
    <author>
      <name>Simone Cinti</name>
    </author>
    <id>https://liferay.dev/nl/c/message_boards/find_message?p_l_id=119785294&amp;messageId=96255446</id>
    <updated>2017-10-04T12:04:09Z</updated>
    <published>2017-10-04T12:04:09Z</published>
    <summary type="html">Hi. You said it right but, please note that in particular cases Microsoft&amp;#39;s WCF APIs relys on the order of a &amp;#34;__type&amp;#34; hint attribute which had to be always the first of the json object structure. I don&amp;#39;t know why and accordingly with the JSON specifications this is out of standard.&lt;br /&gt;I&amp;#39;m working on a web application in Liferay 7 which have to call some remote services based on Microsoft&amp;#39;s .NET technology  (  &lt;img alt="emoticon" src="@theme_images_path@/emoticons/sad.gif" &gt; &lt;img alt="emoticon" src="@theme_images_path@/emoticons/sad.gif" &gt; &lt;img alt="emoticon" src="@theme_images_path@/emoticons/sad.gif" &gt;  ) using RESTful approach,  and for the &amp;#39;BUG&amp;#39; 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.&lt;br /&gt;So it will be great, even for increasing the &amp;#39;interoperability&amp;#39; between systems in the real-world, if Liferay&amp;#39;s will use ordered structures that just keeps the order of insertions in the JSON objects map, even if it&amp;#39;s a non-sense according to the JSON standard. &lt;br /&gt;They only just have to mantain the same order of insertions in map...it&amp;#39;s not so much!&lt;br /&gt;&lt;br /&gt;For more information about that issue, please also see: &lt;a href="https://docs.microsoft.com/en-us/dotnet/framework/wcf/feature-details/stand-alone-json-serialization"&gt;https://docs.microsoft.com/en-us/dotnet/framework/wcf/feature-details/stand-alone-json-serialization&lt;/a&gt;</summary>
    <dc:creator>Simone Cinti</dc:creator>
    <dc:date>2017-10-04T12:04:09Z</dc:date>
  </entry>
  <entry>
    <title>RE: JSONObject comes out in bad order</title>
    <link rel="alternate" href="https://liferay.dev/nl/c/message_boards/find_message?p_l_id=119785294&amp;messageId=4681259" />
    <author>
      <name>Amos Fong</name>
    </author>
    <id>https://liferay.dev/nl/c/message_boards/find_message?p_l_id=119785294&amp;messageId=4681259</id>
    <updated>2010-03-04T18:52:16Z</updated>
    <published>2010-03-04T18:52:16Z</published>
    <summary type="html">From &lt;a href="http://groups.google.com/group/android-developers/browse_thread/thread/0afcf8622cfe8905"&gt;http://groups.google.com/group/android-developers/browse_thread/thread/0afcf8622cfe8905&lt;/a&gt;&lt;br /&gt;&lt;br /&gt;Your code is incorrect if you&amp;#39;re relying on ordering. I&amp;#39;ll get to&lt;br /&gt;that, but first some background which may come in handy:&lt;br /&gt;&lt;br /&gt;HashMaps are unordered. If you want to preserve order, there&amp;#39;s&lt;br /&gt;LinkedHashMap, which retains the order, so when you iterate over the&lt;br /&gt;map, you get the order in which they were inserted. And there&amp;#39;s&lt;br /&gt;TreeMap, which will enforce a sorting order for you, independent of&lt;br /&gt;what order they&amp;#39;re supplied in.&lt;br /&gt;&lt;br /&gt;Unfortunately, that doesn&amp;#39;t help you, since the JSONObject&lt;br /&gt;implementation uses HashMap.&lt;br /&gt;But you can iterate over the keys you get back with JSONObject.keys(),&lt;br /&gt;and store the keys in a TreeSet, sorted according to your preferences,&lt;br /&gt;or in an ArrayList, and call Collections.sort() on it, and then&lt;br /&gt;iterate over the result. That&amp;#39;s a simple, robust solution, if the&lt;br /&gt;client knows what order the keys should be in.&lt;br /&gt;&lt;br /&gt;But that doesn&amp;#39;t give you the original order. The options I see there&lt;br /&gt;are:&lt;br /&gt;&lt;br /&gt;* A modified version of the JSON code. I don&amp;#39;t recommend it,&lt;br /&gt;especially not to deviate from the standard, but if you have no&lt;br /&gt;control over what you&amp;#39;re being sent, this may be your only&lt;br /&gt;alternative. Just take the current code, and whenever it does &amp;#39;new&lt;br /&gt;HashTable()&amp;#39;, do &amp;#39;new LinkedHashTable()&amp;#39;.&lt;br /&gt;&lt;br /&gt;* Modify your protocol to pass the desired key order explicitly (as&lt;br /&gt;the value of another key on the object, or on some single larger&lt;br /&gt;object, etc.). Call JSONObject.keys(), and iterate over that. This is&lt;br /&gt;the most robust solution, but requires server-side cooperation, and&lt;br /&gt;increases the payload size a bit.&lt;br /&gt;&lt;br /&gt;I may be slightly misremembering, but if I recall correctly,&lt;br /&gt;preservation of the key ordering was NOT part of the Javascript/&lt;br /&gt;ECMAscript standards -- but was so widely implemented and relied on&lt;br /&gt;that in the most recent round they made it part of the standard-to-be.&lt;br /&gt;HOWEVER, JSON is *NOT* Javascript, it is a data interchange language.&lt;br /&gt;From the JSON web site:&lt;br /&gt;&lt;br /&gt;&amp;#34;An object is an unordered set of name/value pairs&amp;#34;&lt;br /&gt;&lt;br /&gt;So this is not a JSON bug; it is behaving as expected, for&lt;br /&gt;compatibility across a wide range of languages. So you&amp;#39;ll either have&lt;br /&gt;to change your protocol, or accommodate your deviation from the&lt;br /&gt;standard.</summary>
    <dc:creator>Amos Fong</dc:creator>
    <dc:date>2010-03-04T18:52:16Z</dc:date>
  </entry>
  <entry>
    <title>JSONObject comes out in bad order</title>
    <link rel="alternate" href="https://liferay.dev/nl/c/message_boards/find_message?p_l_id=119785294&amp;messageId=4681223" />
    <author>
      <name>Michael Poznecki</name>
    </author>
    <id>https://liferay.dev/nl/c/message_boards/find_message?p_l_id=119785294&amp;messageId=4681223</id>
    <updated>2010-03-04T18:37:36Z</updated>
    <published>2010-03-04T18:37:36Z</published>
    <summary type="html">&lt;html&gt;&lt;head&gt;&lt;/head&gt;&lt;body&gt;Hello all,&lt;br&gt;&lt;br&gt;I have:&lt;br&gt;&lt;pre&gt;&lt;code&gt;
JSONObject jsonObject = JSONFactoryUtil.createJSONObject();

jsonObject.put("a", "1");
jsonObject.put("b", "2");
jsonObject.put("c", "3");
jsonObject.put("d", "4");

...

&lt;/code&gt;&lt;/pre&gt;&lt;br&gt;&lt;br&gt;but when I spit out the results I have&lt;br&gt;&lt;br&gt;[{"d":"4","b":"2","c":"3","a":"1"}]&lt;br&gt;&lt;br&gt;&lt;br&gt;How can I get this to come out in the right order without sorting it after?&lt;/body&gt;&lt;/html&gt;</summary>
    <dc:creator>Michael Poznecki</dc:creator>
    <dc:date>2010-03-04T18:37:36Z</dc:date>
  </entry>
</feed>
