java - 在 Java 中,在 Android 上将 2D pojo 数组序列化为 json
问题描述
我正在构建一个国际象棋游戏,我想序列化棋盘并在每次移动后将其发送到服务器。我将使用 JSON 来完成这项任务。
我的游戏的表示方式是我创建的自定义 POJO 的二维数组,名为Piece
.
目前我只是在创建一个 JSON 风格的字符串,而不是实际的 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
编辑二
我试过了:
Gson gson = new GsonBuilder().setPrettyPrinting().serializeNulls().create();
message = gson.toJson(piecesMatrix, Piece.class);
我有:
[ 10-07 00:28:20.591 10899:10899 D/Data =
]
{}
解决方案
是这样的吗……
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());
推荐阅读
- android - 在 CameraX 库中创建预览后,每次都需要 bindToLifecycle() 来打开/关闭/自动闪光吗
- python - 我可以在 Flask 中将对象声明为全局变量吗?
- oracle - 500gb 数据库的 Oracle 表空间
- c# - C# Regex 指定允许的开始和结束条件
- apache-spark - 如何在 Spark 中指定任务超时?
- html - 带有参数的 BIRT 动态 XML
- go - Golang 交叉编译未定义的错误
- javascript - 如何在 React 和 Redux 中更改表单输入值?
- javascript - 如何正确访问传递给函数的 JavaScript 变量的名称?
- kotlin - 为什么流程不执行,没有抛出错误?