简体   繁体   中英

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?

You would need to call the toJSONString() :

os.println(jsonObject.toJSONString());

Otherwise the toString() method is called on jsonObject .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM