如何在不将其保存到磁盘的情况下创建并发送JSON文件?

huangapple go评论83阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2020年8月28日 19:03:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/63632535.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定