java - 转换为 JSONArray 时保持 List<LinkedHashMap<String,Object>> 顺序

标签 java json list arrays

我看过所有关于在 JSONArray 中保持顺序的帖子,但我找不到解决方案。

在此post ,他们说:数组是值的有序集合。

但是当我从我的 List 创建一个 JSONArray 时,我丢失了顺序,我必须保留它。

我已经尝试了所有的解决方案,但没有一个适合我。

我正在使用 org.json 作为 JSON 库来创建 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 的 map :

{_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}

伊斯梅尔

最佳答案

让我们看看您的以下代码,了解到底发生了什么。

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;
}

此方法逐个迭代映射(在您的情况下为有序映射,即 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;
}

这里它只是将该键值放入它自己的映射中,该映射最终基本上是一个 HashMap 。 如您所知,散列映射是无序的。这就是为什么当您查看它的输出时,它基本上不是您插入的顺序。

关于java - 转换为 JSONArray 时保持 List<LinkedHashMap<String,Object>> 顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24884381/

相关文章:

list - 如何从 Scala 中的 hashmap 调用函数

list - 比较 Mathematica 中的两个列表

java - 如何使用HttpClient下载连续数据流?

java - 为什么Tomcat服务器位置属性在Eclipse中是灰色的

php - 创建 "two way"配置文件

android - 在 android spinner 中填充 json 数组

python - 如何压缩两个不同大小的列表,重复较短的列表?

java - 即使前面有对话框,Android 触摸事件也会显示在 View 中

java - 我真的应该像躲避瘟疫一样避免 instanceof 吗?

php - 将 SQLite 数据库放入 JSON 对象中以从 iOS 发送到服务器