I'm writing a loader/saver for a little android game im working on. 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.
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
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. 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:
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
[{"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)
You're trying to store a Java array of JSONArray
s as the pathx
and pathy
attributes of your JSON object. You should store a JSONArray
of JSONArray
s instead.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.