java - 为什么没有写JSON?

标签 java json jackson

这是代码:

public static void init() {
    File file = new File(SimpleMessagesAPI.getMainAPI().getDataFolder(), "config.json");
    try {

        ObjectMapper objectMapper = new ObjectMapper();
        ArrayNode arrayNode = objectMapper.createArrayNode();
        ObjectNode firstObjectNode = objectMapper.createObjectNode();
        ObjectNode secondObjectNode = objectMapper.createObjectNode();

        firstObjectNode.put("period", 5);
        firstObjectNode.put("async", true);
        firstObjectNode.put("country", "Europe/Bucharest");

        secondObjectNode.putArray("broadcast")
                .add("&7Hello players! Now is %server_online players!")
                .add("&eNow is %time")
                .add("&6Thanks for playing on that server!")
                .add("&cHave fun guys! %motd");

        ArrayNode firstArrayNode = objectMapper.createArrayNode();
        firstArrayNode.add(firstObjectNode);

        ArrayNode secondArrayNode = objectMapper.createArrayNode();
        secondArrayNode.add(secondObjectNode);

        ObjectNode principalObjectNode = objectMapper.createObjectNode();
        principalObjectNode.putPOJO("mechanic", firstArrayNode);

        ObjectNode secondarObjectNode = objectMapper.createObjectNode();
        secondarObjectNode.putPOJO("messages", secondArrayNode);

        arrayNode.add(principalObjectNode);
        arrayNode.add(secondarObjectNode);

        if (!file.exists()) {
                file.createNewFile();
                objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(arrayNode);
        }

        /*String json = file.toString();
        JsonNode jsonNode = objectMapper.readTree(json);
        String messages = jsonNode.get("messages").asText();
        String mechanic = jsonNode.get("mechanic").asText();
        System.out.println("Messages: " + messages + "\n\n\n" + "Mechanic: " + mechanic + "\n\n");*/
        //ObjectConfig config = new ObjectConfig(messages, mechanic);
        //objectMapper.writeValue(new File(SimpleMessagesAPI.getMainAPI().getDataFolder() + "config.json"), config);

    } catch (IOException e) {
            e.printStackTrace();
    }
}

这就是我想要创建的 JSON:

{
  "mechanic": {
    "period"          : 5,
    "async"           : true,
    "country"         : "Europe/Bucharest"
  },
  "messages": {
    "broadcast"       : [
      "&7Hello players! Now is %server_online players!",
      "&eNow is %time",
      "&6Thanks for playing on that server!",
      "&cHave fun guys! %motd"
    ]
  }
}

但是当我执行代码时,这创建了一个空的 JSON 对象,为什么?

并且 file.createNewFile(); 表示“结果被忽略”。

非常感谢大家的帮助

最佳答案

您的代码行有 2 个问题

file.createNewFile();
objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(arrayNode);
  1. 您在 file.createNewFile(); 上收到编译器警告: 结果被忽略,因为您忽略了返回的 boolean 结果 file.createNewFile().

    你最好应该这样做:

    if (!file.createNewFile())
        throw new IOException("could not create file " + file);
    
  2. 您的方法调用.writeValueAsString(arrayNode)只是 生成一个 JSON 字符串,但它不会将此字符串写入任何地方。

    您需要使用.writeValue(file, arrayNode)相反。

关于java - 为什么没有写JSON?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52004227/

相关文章:

java - "Cannot run program"在程序文件名中使用带有空格的 Runtime.exec 时

java - Jackson 解析器到 Java 对象

php - 使用 PHP 查找 JSON URL 响应中的特定条目

ruby - 如何在 ruby​​ on rails 中创建 json 对象?

json - 通过 Cloud Formation 控制数据库安全组端口访问

java - Jackson(JSON 库)可以自动生成用于反序列化的 POJO 吗?

java - 将 JsonNode 读入预先存在的 POJO

java - @ComponentScan basePackageClasses 有命名约定吗?

java - 将相同的对象添加到上下文和列表中

java - scala.Array[Long] 不符合预期类型 scala.Array[java.lang.Long]