【发布时间】:2019-03-01 14:08:08
【问题描述】:
我正在开发一个 Java 版本的棋盘游戏 RoboRally,我目前正在开发一个类,该类解析 JSON 文档作为生成游戏棋盘的一部分。它几乎可以按预期工作,但并不完全。
JSON 文档被格式化为一个坐标系,其中包含有关板上的内容及其位置的信息。方法 generateJsonBoard() 应该解析文档中的信息。它返回一个 ICell[][] 数组,其中数组中的每个 Cell 对象都包含一个单元格顶部的片段列表。
该方法几乎可以工作,但是当 JSON 数组中有多个元素时会遇到问题。它似乎只选择数组的最后一个元素,并将其添加到列表中,而忽略其他元素。
我的问题真的归结为是否有人明白为什么我的 Cell 对象中的 List 列表只有一个元素,即使 JSON 数组包含多个元素(例如在 location[0][0] 中)。
这是 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;
}
}
【问题讨论】:
-
既然有这么多可以即时创建的 json 库,为什么还要尝试手动重新创建?
-
我确实使用 simplejson 库。跳过导入行
-
好的,但是将一个(ny)Java 对象转换为 json 是一行。将 json 转换为(自定义)java 对象列表时,我们谈论的是四行代码(如果不是更少的话)。为什么有这么多代码?
-
考虑使用 Jackson 来解析 JSON。另一种选择是 GSON。两者都已经解决了这个问题。
标签: java json parsing simplejson