5

I have a Map, which contains String keys and String and integers values. I put values into the map as follows:

Map map = new LinkedHashMap();
map.put("b", 1);
map.put("a", 2);

After this, I added the map into a List:

List out = new LinkedList();
out.add(map);

And after that, I'm created a JSONObject and put the List into it:

org.json.JSONObject json = new org.json.JSONObject();
json.put("header", "header");
json.put("array", out);

But if I do this, I see this json structure:

{"header":"header", "array":[{"a":2,"b":1}]}

But I want to see:

{"header":"header","array":[{"b":1,"a":2}]}

Where did I go wrong? Maybe this isn't the correct way?

UrsinusTheStrong
  • 1,239
  • 1
  • 16
  • 33
Giymose
  • 201
  • 1
  • 6
  • 21

3 Answers3

3

If you want to keep the order, you should not use org.json library as it stores the data in HashMap, so any order you want to preserve will be ignored. There's no easy way to fix this with org.json. I'd suggest you to use another JSON library (GSON, Jackson, minimal-json, etc.). Almost any other library preserves the insertion order like LinkedHashMap.

Tagir Valeev
  • 97,161
  • 19
  • 222
  • 334
  • Can you give me advice about GSON or Jackson and some light example? – Giymose Jul 30 '15 at 05:36
  • @Giymose, check [this question](http://stackoverflow.com/q/12539489/4856258) for Jackson. I personally use minimal-json. – Tagir Valeev Jul 30 '15 at 05:40
  • Thanks Do you have pretty example of minimal-json? If I want to cast this object to org.json.JSONObject - will be it correct? – Giymose Jul 30 '15 at 05:43
  • @Giymose, No, you cannot cast them to org.json.JSONObject. It's better to forget about org.json and use a single alternative library only. – Tagir Valeev Jul 30 '15 at 06:13
0

No mistake. JSON by default sorts peers into alphabetical order. Because each term is explicitly named, the order makes no difference at all. "b=1" and "a=2" in both cases.

TeasingDart
  • 371
  • 1
  • 6
  • [Duplicate](http://stackoverflow.com/questions/3330989/order-of-serialized-fields-using-json-net) has more info. – TeasingDart Jul 30 '15 at 05:17
  • so, I can't data into JSON in the correct sequence I need? – Giymose Jul 30 '15 at 05:21
  • Short answer: No. Slightly longer: Why would you want to? It doesn't matter at all to JSON. Long answer: Yes, anything is possible. See the link above. – TeasingDart Jul 30 '15 at 05:24
  • 1
    @TeasingDart, your link is about C# libraries which are irrelevant here. The OPs problem is about concrete org.json Java implementation, not about JSON format itself. And "default alphabetical order" is just wrong. In OPs case it's HashMap order which is unspecified and implementation-dependent. – Tagir Valeev Jul 30 '15 at 05:25
0

You can't maintain order in Map using JSONObject.

If it is feasible then I would suggest to use List (as it maintains the order) and prepare map on client side for e.g. in java script code.

Refer Array of objects vs Object of Objects

Community
  • 1
  • 1
Naman Gala
  • 4,670
  • 1
  • 21
  • 55