0

so I have a java Server Client Socket running, and I am trying to send a Json Object over the line. I run into a problem when I try to send the Json object. Because the Json object is not serialized. So what I do then is send it as a String to the Client, this works, but I need to rebuild the Json Object tree.

How do I rebuild a Json Object Tree after I send a Stringed version of the Json Object?

here is some code

Server Side

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

Client Side

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

The Json is generated semi manually

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();

Output the client receives

{"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"}
4
  • Use a JSON parser/generator. Commented Oct 9, 2013 at 22:17
  • Show us what you tried, and what does the string that you send look like? Are you generating the JSON by hand? Commented Oct 9, 2013 at 22:18
  • What does the clientData.toString() do? generate JSON? Commented Oct 9, 2013 at 22:19
  • clientData is a Json Object, when I run toString() it turns it into the Stringed form. I will upload the output Commented Oct 9, 2013 at 22:20

1 Answer 1

3

Use a library like Jackson: https://github.com/FasterXML/jackson. With this you can generate as well as parse the JSON.

An alternative to Jackson is Genson: http://owlike.github.io/genson/

Sign up to request clarification or add additional context in comments.

1 Comment

can you please check my question [stackoverflow.com/questions/42024158/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.