我正在寫一個加載程序/保護程序,用於一個小型的Android遊戲im工作。我將它保存爲json文件,爲我的遊戲中的每個「actor」創建一個json對象,將它們放入jsonarray中,然後將jsonarray.tojsonstring()
寫入文件。加載包含數組元素的json文件時發生錯誤
這裏是我如何保存,我稱之爲節電層次方法,增加了每個演員使用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庫並試圖弄清楚事情。繼承人代碼使用的一個片段來測試是否它的加載右
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"));
}
當加載具有與多個movepaths演員(因此其具有內部行動者的主陣列的元件的一個陣列)我得到任何水平在 JSONArray array = ....
行一個錯誤,指出:
Unexpected character (L) at position 50.
繼承人從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 )
你正在使用哪個json庫? – SteveP 2013-03-15 07:03:17