因此,我正在构建一个国际象棋游戏,我想对板进行序列化,并在每次移动后将其发送到服务器。
我将使用JSON执行此任务。
我的游戏表示方式是我创建的名为Piece
的自定义POJO的二维数组。
此刻,我只是创建一个JSON式的String,而不是实际的JSON,这是有问题的,因为我现在不能将其像真正的JSON一样对待,也无法对其进行索引等等。
这是我用来创建此JSON式结构的代码:
StringBuilder result = new StringBuilder();
for (int i = 0; i < piecesMatrix.length; i++) {
result.append("\"row " + i + "\":\n");
for (int j = 0; j < piecesMatrix[i].length; j ++) {
result.append("\"column " + j + "\": ");
if (piecesMatrix[i][j] == null || piecesMatrix[i][j] instanceof LegalMove) {
result.append(" \"null\",\n");
} else {
result.append(" \"" + piecesMatrix[i][j].getClass().getSimpleName() + "\",\n");
}
}
}
message = result.toString();
结果呈现如下:
[ 10-06 20:35:19.971 7509: 7509 D/Data =
]
"row 0":
"column 0": "BlackRook",
"column 1": "BlackKnight",
"column 2": "BlackBishop",
"column 3": "BlackQueen",
"column 4": "BlackKing",
"column 5": "BlackBishop",
"column 6": "BlackKnight",
"column 7": "BlackRook",
"row 1":
"column 0": "BlackPawn",
"column 1": "BlackPawn",
"column 2": "BlackPawn",
"column 3": "BlackPawn",
"column 4": "BlackPawn",
"column 5": "BlackPawn",
"column 6": "BlackPawn",
"column 7": "BlackPawn",
"row 2":
"column 0": "null",
"column 1": "null",
"column 2": "null",
"column 3": "null",
"column 4": "null",
"column 5": "null",
"column 6": "null",
"column 7": "null",
"row 3":
"column 0": "null",
"column 1": "null",
"column 2": "null",
"column 3": "null",
"column 4": "null",
"column 5": "null",
"column 6": "null",
"column 7": "null",
"row 4":
"column 0": "null",
"column 1": "WhitePawn",
"column 2": "null",
"column 3": "null",
"column 4": "null",
"column 5": "null",
"column 6": "null",
"column 7": "null",
"row 5":
"column 0": "null",
"column 1": "null",
"column 2": "null",
"column 3": "null",
"column 4": "null",
"column 5": "null",
"column 6": "null",
"column 7": "null",
"row 6":
"column 0": "WhitePawn",
"column 1": "null",
"column 2": "WhitePawn",
"column 3": "WhitePawn",
"column 4": "WhitePawn",
"column 5": "WhitePawn",
"column 6": "WhitePawn",
"column 7": "WhitePawn",
"row 7":
"column 0": "WhiteRook",
"column 1": "WhiteKnight",
"column 2": "WhiteBishop",
"column 3": "WhiteQueen",
"column 4": "WhiteKing",
"column 5": "WhiteBishop",
"column 6": "WhiteKnight",
"column 7": "WhiteRook",
问题是,如何将其转换为真实的JSON?
理想情况下,我希望输出看起来像这样:
"row 0":
"column 0": "Rook",
"column 1": "Knight",
"column 2": "Bishop",
...,
"row 1":
"column 0": "Pawn",
"column 1": "null",
"column 2": "Pawn",
...,
"row 2":
"column 0": "null",
"column 1": "Pawn",
"column 2": "null",
...
编辑:
尝试:
Gson gson = new
GsonBuilder().setPrettyPrinting().serializeNulls().create();
message = gson.toJson(piecesMatrix);
明白了:
SecurityException: Can not make a java.lang.reflect.Method constructor accessible
EDIT II
尝试:
Gson gson = new GsonBuilder().setPrettyPrinting().serializeNulls().create();
message = gson.toJson(piecesMatrix, Piece.class);
明白了:
[ 10-07 00:28:20.591 10899:10899 D/Data =
]
{}
答案 0 :(得分:1)
像这样...
JSONArray arr = new JSONArray();
for (int i = 0; i < piecesMatrix.length; i++) {
JSONArray innerArray = new JSONArray();
for (int j = 0; j < piecesMatrix[i].length; j++) {
JSONObject column = new JSONObject();
if (piecesMatrix[i][j] == null || piecesMatrix[i][j] instanceof LegalMove) {
try {
column.put("column " + String.valueOf(j), null);
} catch (Exception e) {}
} else {
try {
column.put("column " + String.valueOf(j), piecesMatrix[i][j].getClass().getSimpleName());
} catch (Exception e) {}
}
innerArray.put(column);
}
JSONObject json = new JSONObject();
try {
json.put("row " + i, innerArray);
} catch (JSONException e) {}
arr.put(json);
}
Log.d("Data = \n\n", arr.toString());