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?

有帮助吗?

解决方案

You would need to call the toJSONString():

os.println(jsonObject.toJSONString());

Otherwise the toString() method is called on jsonObject.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top