[英]error loading json file containing array with an array element
I'm writing a loader/saver for a little android game im working on. 我正在为一个正在运行的小android游戏编写装载程序/保存程序。 I'm saving it as a json file by creating a json object for each 'actor' in my game, putting them into a jsonarray, then writing the jsonarray.tojsonstring()
to the file. 我通过为游戏中的每个“ actor”创建一个json对象并将其放入jsonarray,然后将jsonarray.tojsonstring()
写入文件,将其保存为json文件。
here is how I'm saving, I call the save level method which adds each actor to the array using addActor()
then calls the writefile()
method 这就是我的保存方式,我调用保存级别方法,该方法使用addActor()
将每个actor添加到数组中,然后调用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);
}
Sorry, theres a lot of random stuff in there from when I was figuring out the json library and trying to figure things out. 抱歉,自从我弄清楚json库并试图弄清楚这些东西以来,那里有很多随机的东西。 heres a snippet of code use to test if its loading right 这是用于测试是否正确加载的代码片段
JSONArray array = (JSONArray) parser.parse(new FileReader("C:\\Users\\mojo\\Desktop\\svn\\Ball\\src\\com\\moe\\ball\\levels\\" + name + ".json"));
JSONObject obj;
for (int i = 0; i < array.size(); i++) {
obj = (JSONObject) array.get(i);
System.out.println("bodyname " + i + ": " + obj.get("bodyname"));
}
When loading any level that has an actor with multiple movepaths(which therefore has an array inside one of the elements of the main array of actors) I get an error on the JSONArray array = ....
line that says: 当加载具有带有多个movepaths的actor的任何关卡时(因此在actor的主数组的元素之一中包含一个数组),我在JSONArray array = ....
上收到一条错误消息,内容为:
Unexpected character (L) at position 50.
heres a little snippet from the json file so you can see what it looks like where it is causing problems 这是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.
Sorry if this is a stupid question I'm very new to this, I did spend hours searching and if I dont find an answer here I'm just going to try a different json library(I'm using json.simple now) 抱歉,如果这是一个愚蠢的问题,我对此很陌生,我确实花了数小时进行搜索,如果在这里找不到答案,我将尝试使用其他json库(我现在使用json.simple)
You're trying to store a Java array of JSONArray
s as the pathx
and pathy
attributes of your JSON object. 您正在尝试将JSONArray
的Java数组存储为JSON对象的pathx
和pathy
属性。 You should store a JSONArray
of JSONArray
s instead. 您应该改为存储JSONArray
的JSONArray
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.