Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/335.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在添加ArrayList时修复属性名称<;列表<;整数>&燃气轮机;先到JSONArray,再到JSONObject_Java_Json_Gson - Fatal编程技术网

Java 如何在添加ArrayList时修复属性名称<;列表<;整数>&燃气轮机;先到JSONArray,再到JSONObject

Java 如何在添加ArrayList时修复属性名称<;列表<;整数>&燃气轮机;先到JSONArray,再到JSONObject,java,json,gson,Java,Json,Gson,我在将结果保存到文件时遇到问题。我有两个数组列表 ArrayList<List<Integer>>positions ArrayList<List<Integer>>positions2 我想将此数据保存为JSON文件格式,如下所示: [[0,32],[39,19],[60,15],...] "collocation": { "first": [[0,32],[39,19],[60,15],...], "second": [[0,32

我在将结果保存到文件时遇到问题。我有两个数组列表

ArrayList<List<Integer>>positions 
ArrayList<List<Integer>>positions2   
我想将此数据保存为JSON文件格式,如下所示:

[[0,32],[39,19],[60,15],...]
"collocation": {
"first": [[0,32],[39,19],[60,15],...],
"second":  [[0,32],[39,19],[60,15],...]}
{"collocation":{"first":[[10,20],[3,6]],"second":[[80,76],[12,65]]}}
我尝试了以下代码来创建第一个对象

JSONArray JsonArray = new JSONArray(positions);
JSONObject Jsonobject = new JSONObject();
Jsonobject.put("first",JsonArray);
String jsooo = new Gson().toJson(Jsonobject);
最后我得到了结果:

{"map":{"first":{"myArrayList":[{"myArrayList":[0,32]},{"myArrayList":[39,19]},{"myArrayList":[60,15]}}
为什么我会得到“地图”和“myArrayList”以及如何避免/删除它以获得我想要的


那么,我需要做什么才能得到我需要的格式呢?只有在我执行put()时才会发生这种情况,但我不知道其他创建所需结构的方法。

问题是您正试图将
ArrayList
直接存储到
JSONArray
中。GSON正在尝试存储一个
List
对象数组,如果不创建
JSONObject
来保存它,它就不知道如何执行该操作

要解决这个问题,只需在数组中循环,为每个维度创建JSONArray对象并将它们存储到一个对象中

    public static JSONObject saveValues(ArrayList<List<Integer>> pos1, ArrayList<List<Integer>> pos2)
        throws JSONException {
    JSONObject obj = new JSONObject();
    JSONObject collocation = new JSONObject();
    JSONArray first = new JSONArray();
    JSONArray second = new JSONArray();

    for (int i = 0; i < pos1.size(); i++) {
        JSONArray arr = new JSONArray();
        for (int j = 0; j < pos1.get(i).size(); j++) {
            arr.put(pos1.get(i).get(j));
        }
        first.put(arr);
    }
    for (int i = 0; i < pos2.size(); i++) {
        JSONArray arr = new JSONArray();
        for (int j = 0; j < pos2.get(i).size(); j++) {
            arr.put(pos2.get(i).get(j));
        }
        second.put(arr);
    }

    collocation.put("first", first);
    collocation.put("second", second);
    obj.put("collocation", collocation);

    return obj;
}