gpt4 book ai didi

java - 使用 Jackson 将 Java 对象实例写入 YAML

转载 作者:搜寻专家 更新时间:2023-10-31 08:21:57 24 4
gpt4 key购买 nike

我有一个“示例”Pojo 类,如下所述。任何人都可以使用 Jackson 将 Example 类的实例保存到 YAML 文件中。

public class Example {

String name;
int value;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public int getValue() {
return value;
}

public void setValue(int value) {
this.value = value;
}

最佳答案

jackson 有一个 module that supports YAML .确保添加 required dependency到您的项目,然后您可以按如下方式使用它:

// Create an ObjectMapper mapper for YAML
ObjectMapper mapper = new ObjectMapper(new YAMLFactory());

// Write object as YAML file
mapper.writeValue(new File("/path/to/yaml/file"), example);

或者你可以把你的对象写成一个字符串:

// Write object as YAML string
String yaml = mapper.writeValueAsString(example);

关于java - 使用 Jackson 将 Java 对象实例写入 YAML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46445834/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com