I am using the JSON library provided here http://www.json.org/java/index.html to convert a json string I have to CSV. But the problem I have is, the order of the keys is lost after conversion.
This is the conversion code:
JSONObject jo = new JSONObject(someString);
JSONArray ja = jo.getJSONArray("items");
String s = CDL.toString(ja);
System.out.println(s);
This is the content of "someString":
{
"items":
[
{
"WR":"qwe",
"QU":"asd",
"QA":"end",
"WO":"hasd",
"NO":"qwer"
},
]
}
This is the result:
WO,QU,WR,QA,NO
hasd,asd,qwe,end,qwer
While what I expect is to keep the order of the keys:
WR,QU,QA,WO,NO
qwe,asd,end,hasd,qwer
Is there any way I can have this result using this library? If not, is there any other library that will provide the capability to keep the order of keys in the result?
I know this is solved and the question was asked long time ago, but as I'm dealing with a similar problem, I would like to give a totally different approach to this:
For arrays it says "An array is an ordered collection of values." at http://www.json.org/ - but objects ("An object is an unordered set of name/value pairs.") aren't ordered.
I wonder why that object is in an array - that implies an order that's not there.
So a solution would be to put the keys in a "real" array and add the data as objects to each key like this:
So this is an approach that tries to rethink the original modelling and its intent. But I haven't tested (and I wonder) if all involved tools would preserve the order of that original JSON array.
There are (hacky) ways to do it ... but you shouldn't.
In JSON, an object is defined thus:
See http://json.org.
Most implementations of JSON make no effort to preserve the order of an object's name/value pairs, since it is (by definition) not significant.
If you want order to be preserved, you need to redefine your data structure; e.g.
or more simply:
FOLLOWUP
You need to have a hard conversation with whoever designed that file structure and won't let you change it. It is / they are plain wrong. You need to convince them.
If they really won't let you change it:
This kind of thing as really bad. On the one hand, your software will be violating a well established / long standing specification that is designed to promote interoperability. On the other hand, the nit-wits who designed this lame (not JSON!) file format are probably slagging off other people's systems etc 'cos the systems cannot cope with their nonsense.
UPDATE
It is also worth reading what the JSON RFC (RFC 7159) says on this subject. Here are some excerpts:
It is quite simple to maintain order. I had the same problem with maintaining the order from DB layer to UI Layer.
Open JSONObject.java file. It internally uses HashMap which doesn't maintain the order.
Change it to LinkedHashMap:
This worked for me. Let me know in the comments. I suggest the JSON library itself should have another JSONObject class which maintains order, like JSONOrderdObject.java. I am very poor in choosing the names.
patchFor(answer @gary) :
Tested the wink solution, and working fine:
Another hacky solution using reflect: