【问题标题】:How to send a Json Object in java如何在java中发送一个Json对象
【发布时间】:2013-10-17 12:07:35
【问题描述】:

所以我有一个 java 服务器客户端套接字正在运行,我试图通过线路发送一个 Json 对象。当我尝试发送 Json 对象时遇到问题。因为 Json 对象没有被序列化。所以我要做的就是将它作为一个字符串发送给客户端,这可行,但我需要重建 Json 对象树。

发送字符串版本的 Json 对象后如何重建 Json 对象树?

这里有一些代码

服务器端

//Snip
    //Inside loop
    out.writeObject(clientData.toString());
//End Snip

客户端

//Snip
    while ((fromServer = objectIO.readObject()) != null) {
            commands.interpretServer(fromServer);
            sleepThread(100);
            if (fromServer.equals("Bye.")){
                break;
            }
    }
//End Snip

Json 是半手动生成的

clientData = Json.createObjectBuilder()
                    .add("playerX", playerContent[0][1])
                    .add("playerY", playerContent[1][1])
                    .add("playerTotalHealth", playerContent[2][1])
                    .add("playerCurrentHealth", playerContent[3][1])
                    .add("playerTotalMana", playerContent[4][1])
                    .add("playerCurrentMana", playerContent[5][1])
                    .add("playerExp", playerContent[6][1])
                    .add("playerExpTNL", playerContent[7][1])
                    .add("playerLevel", playerContent[8][1])
                    .add("points", playerContent[9][1])
                    .add("strength", playerContent[10][1])
                    .add("dexterity", playerContent[11][1])
                    .add("constitution", playerContent[12][1])
                    .add("intelligence", playerContent[13][1])
                    .add("wisdom", playerContent[14][1])
                    .add("items", Json.createArrayBuilder()
                            .add(items[0]).add(items[1])
                            .add(items[2]).add(items[3])
                            .add(items[4]).add(items[5])
                            .add(items[6]).add(items[7])
                            .add(items[8]).add(items[9])
                            .add(items[10]).add(items[11])
                            .add(items[12]).add(items[13])
                            .add(items[14]).add(items[15])
                            .add(items[16]).add(items[17])
                            .add(items[18]).add(items[19])
                            .add(items[20]).add(items[21])
                            .add(items[22]).add(items[23])
                            .build())
                    .add("currentMapX", playerContent[16][1])
                    .add("currentMapY", playerContent[17][1])
                    .build();

客户端接收到的输出

{"playerX":"2*32","playerY":"7*32","playerTotalHealth":"100","playerCurrentHealth":"100","playerTotalMana":"50","playerCurrentMana":"50","playerExp":"0","playerExpTNL":"20","playerLevel":"1","points":"0","strength":"1","dexterity":"1","constitution":"1","intelligence":"1","wisdom":"1","items":["1","2","3","4","5","6","7","8","9","10","11","12","13","14","15","16","17","18","19","20","21","22","23","24"],"currentMapX":"0","currentMapY":"0"}

【问题讨论】:

  • 使用 JSON 解析器/生成器。
  • 向我们展示您尝试了什么,您发送的字符串是什么样的?您是手动生成 JSON 吗?
  • clientData.toString() 是做什么的?生成 JSON?
  • clientData 是一个 Json 对象,当我运行 toString() 时,它会将其转换为字符串形式。我将上传输出

标签: java json client-server


【解决方案1】:

使用像 Jackson 这样的库:https://github.com/FasterXML/jackson。有了它,您可以生成和解析 JSON。

Jackson 的替代品是 Genson:http://owlike.github.io/genson/

【讨论】:

猜你喜欢
  • 2020-04-02
  • 2022-08-22
  • 1970-01-01
  • 1970-01-01
  • 2017-09-30
  • 1970-01-01
  • 2014-03-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多