如何使用java将yaml文件转换为数组

发布于 2024-12-29 10:57:12 字数 130 浏览 0 评论 0原文

我需要将 yaml 文件数据访问到 java 文件中。 我使用了 YamlReader 类,现在 yaml 文件已加载到 java 类对象中。 现在所有信息都在对象中,我想从该对象中提取它。我该怎么做。 任何人都可以帮助我吗?我被这个问题困扰了。

i need to access the yaml file data into the java file.
i used the YamlReader class and now the yaml file is loaded into the java class object.
now all the information is in object and i want to extract it from this object.How can i do this .
Can any one help me please i am stuck with this problem.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

愿与i 2025-01-05 10:57:12

您可以阅读地图:

YamlReader reader = new YamlReader(new FileReader("contact.yml"));
Object object = reader.read();
System.out.println(object);
Map map = (Map)object;
System.out.println(map.get("address"));

来源:http://code.google.com/p/yamlbeans/< /a>

You can read into a Map:

YamlReader reader = new YamlReader(new FileReader("contact.yml"));
Object object = reader.read();
System.out.println(object);
Map map = (Map)object;
System.out.println(map.get("address"));

Source: http://code.google.com/p/yamlbeans/

左耳近心 2025-01-05 10:57:12
Yaml yaml = new Yaml();
String document = "\n- Hesperiidae\n- Papilionidae\n- Apatelodidae\n- Epiplemidae";
List<String> list = (List<String>) yaml.load(document);
System.out.println(list);
Yaml yaml = new Yaml();
String document = "\n- Hesperiidae\n- Papilionidae\n- Apatelodidae\n- Epiplemidae";
List<String> list = (List<String>) yaml.load(document);
System.out.println(list);
救星 2025-01-05 10:57:12

使用snakeyaml,下面的代码对我不起作用。当 yaml 对象返回我的对象​​时,它返回一个 LinkedHashMap 类型。因此,我将返回的对象转换为 LinkedHashMap。

public class YamlConfig {

    Yaml yaml = new Yaml();
    String path = "blah blah";
    InputStream input = new FileInputStream(new File(this.path));
    LinkedHashMap<String,String> map;

    @SuppressWarnings("unchecked")
    private void loadHashMap() throws IOException {
        Object data = yaml.load(input);// load the yaml document into a java object
         this.map = (LinkedHashMap<String,String>)data;
         System.out.println(map.entrySet()); //use this to look at your hashmap keys
         System.out.println(map);//see the entire map
    }

Using snakeyaml, the code below didn't work for me. When the yaml object returned my object, it returned a LinkedHashMap type. So I cast my returned object into a LinkedHashMap.

public class YamlConfig {

    Yaml yaml = new Yaml();
    String path = "blah blah";
    InputStream input = new FileInputStream(new File(this.path));
    LinkedHashMap<String,String> map;

    @SuppressWarnings("unchecked")
    private void loadHashMap() throws IOException {
        Object data = yaml.load(input);// load the yaml document into a java object
         this.map = (LinkedHashMap<String,String>)data;
         System.out.println(map.entrySet()); //use this to look at your hashmap keys
         System.out.println(map);//see the entire map
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文