Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/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 加载包含带有数组元素的数组的json文件时出错_Java_Json_Arrays - Fatal编程技术网

Java 加载包含带有数组元素的数组的json文件时出错

Java 加载包含带有数组元素的数组的json文件时出错,java,json,arrays,Java,Json,Arrays,我正在为我正在开发的一款android小游戏编写一个加载程序/保护程序。通过为游戏中的每个“参与者”创建一个json对象,将它们放入jsonarray,然后将jsonarray.tojsonstring()写入该文件,我将其保存为一个json文件 下面是我如何保存的,我调用save-level方法,该方法使用addActor()将每个参与者添加到数组中,然后调用writefile()方法 public void addActor(Actor actor, int index, JSONA

我正在为我正在开发的一款android小游戏编写一个加载程序/保护程序。通过为游戏中的每个“参与者”创建一个json对象,将它们放入jsonarray,然后将
jsonarray.tojsonstring()
写入该文件,我将其保存为一个json文件

下面是我如何保存的,我调用save-level方法,该方法使用
addActor()
将每个参与者添加到数组中,然后调用
writefile()
方法

    public void addActor(Actor actor, int index, JSONArray actorArray) {
    if (actor != null) {
        System.out.println("adding actor " + actor.name);
    } else {
        System.out.println("Adding nulla ctor");
    }
    if (actors[index] != null) {
    } else {
        actors[index] = new JSONObject();
        actors[index].put("index", index);
        actors[index].put("bodyname", actor.name);
        actors[index].put("restitution", actor.body.getFixtureList().get(0).getRestitution());
        actors[index].put("density", actor.body.getFixtureList().get(0).getDensity());
        actors[index].put("friction", actor.body.getFixtureList().get(0).getFriction());
        actors[index].put("startx", actor.startX);
        actors[index].put("starty", actor.startY);
        actors[index].put("startrotation", actor.startAngle);
        actors[index].put("rotationspeed", actor.rotSpeed);
        actors[index].put("enabled", actor.enabled);
        actors[index].put("numPaths", actor.numPaths);
        if (actor.numPaths > 0) {
            JSONArray[] pathx = new JSONArray[actor.getNumPaths()];
            JSONArray[] pathy = new JSONArray[actor.getNumPaths()];
            JSONArray[] pathspeed = new JSONArray[actor.getNumPaths()];
            for (int i = 0; i < actor.getNumPaths(); i++) {
                pathx[i] = new JSONArray();
                pathy[i] = new JSONArray();
                pathspeed[i] = new JSONArray();
                for (int j = 0; j < actor.getPath(i).size; j++) {
                    pathx[i].add(actor.getPath(i).points[j].x);
                    pathy[i].add(actor.getPath(i).points[j].y);
                    pathspeed[i].add(actor.getPath(i).points[j].speed);
                }
            }
            System.out.println("added " + actor.name + ", " + pathx.length + " paths.");
            actors[index].put("pathx", pathx);
            actors[index].put("pathy", pathy);
            actors[index].put("pathspeed", pathspeed);
        }
        //indices.add(index);
        actorArray.add(actors[index]);
    }
}


public void writeFile(String name, JSONArray actorArray) {
    try {
        FileWriter writer = new FileWriter("C:\\Users\\mojo\\Desktop\\svn\\Ball\\src\\com\\moe\\ball\\levels\\" + name + ".json");
        writer.write(actorArray.toJSONString());
        writer.flush();
        writer.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

    public void saveLevel(String levelName, BallScreen screen) {
    JSONArray actorArray = new JSONArray();
    for (int i = 0; i < screen.maxActor; i++) {
        addActor(screen.actorList[i], i, actorArray);
    }
    writeFile(levelName, actorArray);

}
下面是json文件中的一个小片段,这样您就可以看到它在导致问题的地方是什么样子

[{"enabled":true,"index":0,"density":0.6,"pathy":[Lorg.json.simple.JSONArray;@39d3da65,"friction":0.6,"rotationspe
the \`L\` in \`[Lorg.json....\` is at position 50.

很抱歉,如果这是一个愚蠢的问题,我对此很陌生,我确实花了几个小时搜索,如果我在这里找不到答案,我将尝试另一个json库(我现在使用的是json.simple)

您正试图将
JSONArray
的Java数组存储为json对象的
pathx
path
属性。您应该存储
JSONArray
s的
JSONArray

您使用的是哪个json库?所以我只是创建一个JSONArray,并将JSONArray添加为元素?是的,这是您应该做的。
Unexpected character (L) at position 50.
[{"enabled":true,"index":0,"density":0.6,"pathy":[Lorg.json.simple.JSONArray;@39d3da65,"friction":0.6,"rotationspe
the \`L\` in \`[Lorg.json....\` is at position 50.