[英]How to write 2 two-dimensional array to json object in java using json-simple?
I'm setting up a java game server request handler that get json messages and send back relevant json message as response.我正在设置一个 java 游戏服务器请求处理程序,它获取 json 消息并将相关的 json 消息作为响应发回。 In some cases I need to send 2 dimensional String array as the game board.
在某些情况下,我需要发送二维字符串数组作为游戏板。 I'm having a problem to do that using json-simple.
我在使用 json-simple 时遇到了问题。 Further more, how to parse it to a board in the client side afterwards?
更进一步,之后如何将其解析为客户端的板子? Thanks.
谢谢。
char[][] charArray; //initialised
JSONObject jsonOut = new JSONObject();
ObjectOutputStream writer = new ObjectOutputStream(socket.getOutputStream());
JSONArray ja = new JSONArray() ;
ja.add(charArray);
jsonOut.put("board", ja);
writer.writeObject(jsonOut);
getting exception while ja.add(charArray); ja.add(charArray) 时出现异常;
You are attempting to add an entire char[][]
array as a single element in JSONArray
.您正在尝试将整个
char[][]
数组添加为JSONArray
的单个元素。 You need to create a multi-dimensional JSONArray
and map the char[][]
character-by-character:您需要创建一个多维
JSONArray
并逐个字符地映射char[][]
:
JSONArray jsonArray = new JSONArray();
for (char[] ca : charArray) {
JSONArray arr = new JSONArray();
for (char c : ca) {
arr.add(Character.toString(c)); // or some other conversion
}
jsonArray.add(arr);
}
你需要有一个 JsonArray 的 JsonArray,就像你的 String[][] 是一个数组数组一样。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.