1

I am using a servlet that returns a JSONArray of JSONObjects via URL:

http://myserver.corp:8080/webtools/myServlet?targetDB=myTargetDB&sqlStatement=myStatement&param0=abc

Output:

[
    {
        "attribute1": "value1",
        "attribute2": "value2",
        "attribute3": "value3",
        "attribute4": "value4",
    }
]

Now this works like a charm, until I discovered that the order attributes varies depending on the environment I launch it in.

When I am launching the program in Eclipse, I get the above result - but when launching the program as a runnable JAR, the order is reversed (attribute4 to attribute1).

Now I understand that JSONObjects are not ordered. But there must be a reason why the order of the attributes in the JSONObjects is reversed.

Does anyone have an idea ?

Cheers, Tim

Rahul Vishwakarma
  • 996
  • 5
  • 17
  • 35
Tim
  • 3,910
  • 8
  • 45
  • 80
  • Who knows? My guess it that they're being stored in something like a `HashMap` and then written in iteration order. – chrylis -cautiouslyoptimistic- Feb 25 '15 at 09:42
  • 2
    Because JSONs are not ordered. If you want them to be ordered, use a list. There is no meaning to the question because what you see when you print a **representation** of the objects is not the objects order. It **has no order**. – Reut Sharabani Feb 25 '15 at 09:42
  • @ReutSharabani 's comment is the answer of this question. If you want to order the items, do it at client side. – Devrim Feb 25 '15 at 09:46
  • I know - thanks. My main interrogation was that I did not just get any order, but the exact reverse order (consistently). I will now check the names of the keys while iterating through them. – Tim Feb 25 '15 at 09:58
  • possible duplicate of [JSON order mixed up](http://stackoverflow.com/questions/3948206/json-order-mixed-up) – Didac Montero Mar 23 '15 at 08:04

3 Answers3

1

From http://json.org (my emphasis):

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

So this

{ "foo": 1, "bar": 2 }

and this

{ "bar": 2, "foo": 1 }

is the same object. Any library that is looking for an order is looking for something that does and can not exist. Code can output a JSON object in any order it pleases.

0

Duplicated: JSONObject : Why JSONObject changing the order of attributes

If you use Jackson you can order fields in your class, if your object is a Java POJO

@JsonPropertyOrder({ "attribute1", "attribute2", "attribute3"})
public class Attributes { ... }
Community
  • 1
  • 1
Didac Montero
  • 2,046
  • 19
  • 27
  • I know there is no problem. The object created will be the same. equals and hashcode will be the same. But he just asks for the order anyway, so I answer with a possibility with other tool. – Didac Montero Feb 25 '15 at 09:51
0

To solve this problem :

Go on class JSONObject
On the method : public JSONObject()
change this.map = new LinkedHashMap();

LinkedHashMap will iterate in the order in which the entries were put into the map

/**
     * Construct an empty JSONObject.
     */
    public JSONObject() {
        this.map = new LinkedHashMap();
    }
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103