英文:
How can I create and send JSON file without saving it to disk?
问题
在将 JSON 文件发送给用户之前(通过 Telegram 机器人),我会将其保存到磁盘上。我是否可以直接创建并将 JSON 文件发送给用户,而无需将其保存到磁盘上呢?
private static final String PATH = "./library/";
public static ObjectMapper objectMapper = new ObjectMapper();
public void toJSON(Task task) throws IOException {
String packageName = taskService.getId(task);
String fileName = packageName + ".json";
String pathName = LIBRARY_PATH + fileName;
Path path = Paths.get(pathName);
if (!Files.exists(path)) {
Files.createDirectories(path.getParent());
Files.createFile(path);
}
objectMapper.writeValue(new File(pathName), googlePlayGame);
}
英文:
Before sending JSON file to user (via Telegram Bot) I save it to disk. Can I just create and send JSON file to user without saving it to disk?
private static final String PATH = "./library/";
public static ObjectMapper objectMapper = new ObjectMapper();
public void toJSON(Task task) throws IOException {
String packageName = taskService.getId(task);
String fileName = packageName + ".json";
String pathName = LIBRARY_PATH + fileName;
Path path = Paths.get(pathName);
if (!Files.exists(path)) {
Files.createDirectories(path.getParent());
Files.createFile(path);
}
objectMapper.writeValue(new File(pathName), googlePlayGame);
}
答案1
得分: 2
objectMapper.writeValueAsString()
将JSON转换为字符串。如果你需要原始字节,还有objectMapper.writeValueAsBytes()
。
英文:
objectMapper.writeValueAsString()
gives you the JSON as a String. There's also objectMapper.writeValueAsBytes()
if you need raw bytes.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论