Question

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?

Was it helpful?

Solution

You would need to call the toJSONString():

os.println(jsonObject.toJSONString());

Otherwise the toString() method is called on jsonObject.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top