Walid Sarkis Walid Sarkis - 5 months ago 17
JSON Question

Sending Json Object from Server to Client JAVA

Given the following JSON object example:

jsonObject.put("Names", jsonArray);

try{

FileWriter jsonFileWriter = new FileWriter(jsonFilePath);
jsonFileWriter.write(jsonObject.toJSONString());
jsonFileWriter.flush();
jsonFileWriter.close();

System.out.print(jsonObject);

} catch (IOException e){
e.printStackTrace();
}


I am using JAVA Server and Client that are using sockets, can I use

os.println(jsonObject);


To send it to the client, or is there another method to send the JSON object?

Answer

You would need to call the toJSONString():

os.println(jsonObject.toJSONString());

Otherwise the toString() method is called on jsonObject.

Comments