I've seen all the posts about keeping order in JSONArray, but I couldn't find a solution.
In this post, they said : An array is an ordered collection of values.
But when I create a JSONArray
from my List
I lost the order and I have to keep it.
I've tried all the solutions, but none of them works for me.
I'm using org.json
as JSON
lib to create JSONArray
and then CDL
to create a CSV
.
The List
contains JSON
so I could create a CSV
file.
Code :
List<LinkedHashMap<String, Object>> stringObject=new ArrayList<LinkedHashMap<String,Object>>();
for(...; ...;...){
stringObject.add(map);
}
/** stringObject is a List containing ordered map */
/** Here I'm trying to create a JSONArray */
JSONARRay jsonArray=new JSONArray(stringObject);
Here the ouput :
The List : [{_id=1, name=Aurelia Menendez, scores.0.type=exam, scores.0.score=60.06045071030959, scores.1.type=quiz, scores.1.score=52.79790691903873, scores.2.type=homework, scores.2.score=71.76133439165544}, {_id=2, name=Corliss Zuk, scores.0.type=exam, scores.0.score=67.03077096065002, scores.1.type=quiz, scores.1.score=6.301851677835235, scores.2.type=homework, scores.2.score=66.28344683278382},{.....}]
The JSONArray : [{"scores.1.type":"quiz","scores.1.score":52.79790691903873,"_id":1,"name":"Aurelia Menendez","scores.0.type":"exam","scores.2.type":"homework","scores.0.score":60.06045071030959,"scores.2.score":71.76133439165544},{"scores.1.type":"quiz","scores.1.score":6.301851677835235,"_id":2,"name":"Corlis ...
As you can see, I lost the order.
So How to keep the right order so I can create a good CSV
?
The map
order is respected, but the items order inside a map
is not respected.
Edits
Maps I'm adding to the List
:
{_id=1, name=Aurelia Menendez, scores.0.type=exam, scores.0.score=60.06045071030959, scores.1.type=quiz, scores.1.score=52.79790691903873, scores.2.type=homework, scores.2.score=71.76133439165544}
{_id=2, name=Corliss Zuk, scores.0.type=exam, scores.0.score=67.03077096065002, scores.1.type=quiz, scores.1.score=6.301851677835235, scores.2.type=homework, scores.2.score=66.28344683278382}
{_id=3, name=Bao Ziglar, scores.0.type=exam, scores.0.score=71.64343899778332, scores.1.type=quiz, scores.1.score=24.80221293650313, scores.2.type=homework, scores.2.score=42.26147058804812}
{_id=4, name=Zachary Langlais, scores.0.type=exam, scores.0.score=78.68385091304332, scores.1.type=quiz, scores.1.score=90.2963101368042, scores.2.type=homework, scores.2.score=34.41620148042529}
{_id=11, name=Marcus Blohm, scores.0.type=exam, scores.0.score=78.42617835651868, scores.1.type=quiz, scores.1.score=82.58372817930675, scores.2.type=homework, scores.2.score=87.49924733328717}
{_id=12, name=Quincy Danaher, scores.0.type=exam, scores.0.score=54.29841278520669, scores.1.type=quiz, scores.1.score=85.61270164694737, scores.2.type=homework, scores.2.score=80.40732356118075}
{_id=15, name=Tambra Mercure, scores.0.type=exam, scores.0.score=69.1565022533158, scores.1.type=quiz, scores.1.score=3.311794422000724, scores.2.type=homework, scores.2.score=45.03178973642521}
Using JSONObject :
JSONObject jsonObject=new JSONObject();
for(LinkedHashMap<String,Object> map:stringObject){
jsonObject.putAll(map);
System.out.println(jsonObject);
}
Output :
{"scores.1.type":"quiz","scores.1.score":52.79790691903873,"_id":1,"name":"Aurelia Menendez","scores.0.type":"exam","scores.2.type":"homework","scores.0.score":60.06045071030959,"scores.2.score":71.76133439165544}
{"scores.1.type":"quiz","scores.1.score":6.301851677835235,"_id":2,"name":"Corliss Zuk","scores.0.type":"exam","scores.2.type":"homework","scores.0.score":67.03077096065002,"scores.2.score":66.28344683278382}
{"scores.1.type":"quiz","scores.1.score":24.80221293650313,"_id":3,"name":"Bao Ziglar","scores.0.type":"exam","scores.2.type":"homework","scores.0.score":71.64343899778332,"scores.2.score":42.26147058804812}
{"scores.1.type":"quiz","scores.1.score":90.2963101368042,"_id":4,"name":"Zachary Langlais","scores.0.type":"exam","scores.2.type":"homework","scores.0.score":78.68385091304332,"scores.2.score":34.41620148042529}
{"scores.1.type":"quiz","scores.1.score":82.58372817930675,"_id":11,"name":"Marcus Blohm","scores.0.type":"exam","scores.2.type":"homework","scores.0.score":78.42617835651868,"scores.2.score":87.49924733328717}
Ismail