我已经看过关于在JSONArray中保持订单的所有帖子,但我找不到解决方案。
在这个post中,他们说:An array is an ordered collection of values.
但是当我从JSONArray
创建List
时,我丢失了订单,我必须保留它。
我已经尝试了所有的解决方案,但它们都不适合我。
我使用org.json
作为JSON
lib来创建JSONArray
,然后使用CDL
来创建CSV
。
List
包含JSON
,因此我可以创建CSV
文件。
代码:
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);
这里的输出:
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 ...
如您所见,我丢失了订单。
那么如何保持正确的顺序,以便创建一个好的CSV
?
尊重map
订单,但不尊重map
内的订单。
编辑
地图我正在添加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}
使用JSONObject:
JSONObject jsonObject=new JSONObject();
for(LinkedHashMap<String,Object> map:stringObject){
jsonObject.putAll(map);
System.out.println(jsonObject);
}
输出:
{"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}
伊斯梅尔
答案 0 :(得分:3)
让我们看看你的下面的代码,并了解到底发生了什么。
JSONObject jsonObject=new JSONObject();
for(LinkedHashMap<String,Object> map:stringObject){
jsonObject.putAll(map);
System.out.println(jsonObject);
}
让我们来看看这里发生了什么...... 当你调用jsonObject.putAll(map).....这就是发生的事情
public JSONObject putAll(Map<String, Object> newProperties)
{
assert newProperties != null;
for (Map.Entry<String, Object> e : newProperties.entrySet())
{
this.put(e.getKey(), e.getValue());
}
return this;
}
此方法逐个迭代map(在您的情况下为Ordered map,即LinkedHashMap)并将其添加到其自己的put方法中,其中键和值作为参数。
现在让我们看看put方法中发生了什么
public JSONObject put(String key, Object value) throws JSONException {
if (key == null) {
throw new NullPointerException("Null key.");
}
if (value != null) {
testValidity(value);
this.map.put(key, value);
} else {
this.remove(key);
}
return this;
}
这里只是将键值放在自己的映射中,最后基本上是一个散列映射。 如您所知,哈希映射不是有序的。这就是为什么当你看它的输出时,它基本上不是你插入的顺序。
答案 1 :(得分:1)
从你的问题我认为你没有得到与输出顺序相同的插入顺序。 我只是仔细检查了一下,发现你错了。我已完成以下操作并获得以下输出。
/*******************output*************/
[{"val0":"val0"},{"val1":"val1"},{"val2":"val2"},{"val3":"val3"},{"val4":"val4"},{"val5":"val5"},{"val6":"val6"},{"val7":"val7"},{"val8":"val8"},{"val9":"val9"}]
/********************************/
/************** Code****************/
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import org.json.JSONArray;
public class TestOrderArrayList {
public static void main(String[] args) {
List<LinkedHashMap<String, Object>> stringObject = new ArrayList<LinkedHashMap<String, Object>>();
for (int i = 0; i < 10; i++) {
LinkedHashMap<String, Object> hashMap = new LinkedHashMap<String, Object>();
hashMap.put("val" + i, "val" + i);
stringObject.add(hashMap);
}
/** stringObject is a List containing ordered map */
/** Here I'm trying to create a JSONArray */
JSONArray jsonArray = new JSONArray(stringObject);
System.out.println(jsonArray);
}
}
如果还有其他想说的话。请详细说明一下。
希望这能解决您的问题。
答案 2 :(得分:1)
你正在寻找错误的地方。列表中的元素保持有序(List
和JSONArray
保持订单)。
但是您要将地图添加到JSONArray
。每个地图中项的转换似乎都失去了排序。您只需将一个地图转换为JSONObject
即可查看。
要回答如何保留此订单,我需要知道您正在使用哪个JSON框架。
编辑您正在使用默认的json.org Java框架。正如文件明确指出:
JSONObject是名称/值对的无序集合。
所以没有办法用这个框架来做。
我检查了杰克逊,但无法找到任何迹象表明它保留了地图元素的排序顺序。
我的猜测是,这是因为JavaScript不支持此功能。
答案 3 :(得分:0)
According the values you specified in the "Output" section, the JsonArray in the JsonObject is retaining the order [same order maintained in the List Object].
You do not have to worry about the order of JSonElements in your response, as according to the JSON specification they need not be maintained.
For example:
JsonString1 = {"id":2,"name":"John"}
JsonString2 = {"name":"John","id":2}
are exactly the same. i.e if you use org.json classes or Jackson to check equality of the above JsonObjects they will be equal.
On the flipside if you want the same order in your JsonArrays every time you run your program, I suppose you should be using a LinkedList<> instead of a List<>.