我正在开发Java版本的棋盘游戏RoboRally,目前正在研究一个类,该类将JSON文档解析为生成游戏棋盘的一部分。它几乎可以按预期工作,但效果不尽人意。
JSON文档的格式设置为坐标系,其中包含有关板上的内容以及其位置的信息。方法generateJsonBoard()应该解析文档中的信息。它返回一个ICell [] []数组,其中该数组上的每个Cell对象在该单元格的顶部都包含一个片段列表。
该方法几乎可以工作,但是当JSON数组中有多个元素时,它将遇到问题。似乎只选择数组的最后一个元素,然后将其添加到列表中,而忽略其他元素。
我的问题真的归结为是否有人看到即使我的JSON数组包含多个元素(例如在location [0] [0]中),为什么Cell对象中的List列表也只有一个元素。
这是JSON文档的一部分
{
"0": {
"0": ["NorthWall","FlagOne","FlagTwo","FlagThree"],
"1": ["NorthWall"],
"2": ["NorthWall"],
"3": ["NorthWall"],
"4": ["NorthWall"],
"5": ["NorthWall"],
"6": ["NorthWall"],
"7": ["NorthWall"],
"8": ["NorthWall"],
"9": ["NorthWall"]
},
"1": {
"0": ["WestWall"],
"1": [],
"2": [],
"3": [],
"4": [],
...
...
这是Java代码。
public class JSONBoardGenerator {
JSONParser parser = new JSONParser();
public ICell[][] generateJsonBoard(String filepath) {
ICell[][] jsonBoardPieceList2 = null;
try {
Object boardFile = parser.parse(new FileReader(filepath));
JSONObject jsonBoardFile = (JSONObject) boardFile;
int jsonSize = jsonBoardFile.size();
int jsonSide = jsonSize / jsonSize;
jsonBoardPieceList2 = new ICell[10][10];
System.out.println(jsonBoardFile);
for (int x = 0; x <= 9; x++) {
for (int y = 0; y <= 9; y++) {
String intX = Integer.toString(x);
String intY = Integer.toString(y);
JSONObject xCord = (JSONObject) jsonBoardFile.get(intX);
JSONArray yCord = (JSONArray) xCord.get(intY);
Iterator<String> iterator = yCord.iterator();
while (iterator.hasNext()) {
Cell tempCell = new Cell();
jsonBoardPieceList2[x][y] = tempCell;
String jsonIterator = iterator.next();
System.out.println(jsonIterator);
switch (jsonIterator) {
case "Hole":
System.out.println("Making Hole!");
break;
case "NorthWall":
System.out.println("Making northfacing wall!");
tempCell.addPiece(new Wall(Direction.NORTH));
break;
case "EastWall":
System.out.println("Making eastfacing wall!");
tempCell.addPiece(new Wall(Direction.EAST));
break;
case "SouthWall":
System.out.println("Making southfacing wall!");
tempCell.addPiece(new Wall(Direction.SOUTH));
break;
case "WestWall":
System.out.println("Making westfacing wall!");
tempCell.addPiece(new Wall(Direction.WEST));
break;
/**
case "ConveyorNorth":
System.out.println("Making northfacing conveyor!");
new Conveyor(Direction.NORTH, 1);
break;
case "ConveyorEast":
System.out.println("Making eastfacing conveyor!");
new Conveyor(Direction.EAST, 1);
break;
case "ConveyorSouth":
System.out.println("Making southfacing conveyor!");
new Conveyor(Direction.SOUTH, 1);
break;
case "ConveyorWest":
System.out.println("Making westfacing conveyor!");
new Conveyor(Direction.WEST, 1);
break;
*/
case "FlagOne":
System.out.println("Making flag number 1");
tempCell.addPiece(new Flag(1));
break;
case "FlagTwo":
System.out.println("Making flag number 2");
tempCell.addPiece(new Flag(2));
break;
case "FlagThree":
System.out.println("Making flag number 3");
tempCell.addPiece(new Flag(3));
break;
}
}
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
}
return jsonBoardPieceList2;
}
}
答案 0 :(得分:0)
把它弄坏了。使用简单的JSON可以正常工作。唯一的问题是我将Cell tempCell = new Cell();太深。它应该在for循环内,而不是while迭代器内。谢谢大家的建议:)