【问题标题】:How do I use snakeyaml as a function that returns something?如何使用snakeyaml 作为返回某些东西的函数?
【发布时间】:2012-04-14 13:44:27
【问题描述】:

我正在尝试使用snakeyaml 制作一个转储int、字符串和字符串[] 的函数。问题是我不知道如何编写函数以便插入信息。

例如:

public void testDump() {
    Map<String, Object> data = new HashMap<String, Object>();
    data.put("name", "Silenthand Olleander");
    data.put("race", "Human");
    data.put("traits", new String[] { "ONE_HAND", "ONE_EYE" });
    Yaml yaml = new Yaml();
    String output = yaml.dump(data);
    System.out.println(output);
}

我需要像 "name""Silenthand Olleander" 这样的东西是可配置的。我也不知道这个函数到底是做什么的。它会创建一个新文件吗?因为我需要它在现有的 strings.yml 文件中添加一行。所以我希望 string.yml 的格式是这样的:

#String.yml file
0 name_here The array of argument messages here.
1 name_here Another array of argument messages here.
2 name_here And again... I think you get the point.

【问题讨论】:

    标签: java yaml snakeyaml


    【解决方案1】:

    由于代码(最终)来自documentation,我假设您已经阅读了。

    调用yaml.dump() 不会创建文件,而是返回一个字符串(然后您可以put into a file)。根据文档,dump 方法将正确处理列表/数组。

    要使 HashTable 中的键(例如“名称”)可配置,您可以将所需的值传递给填充表的函数。

    根据您的问题,尽管您似乎需要学习如何使用 Java 本身进行编程的速成课程,然后才能解决 snakeyaml。

    【讨论】:

    • 我认为它的 Map data = new HashMap();这让我很困惑。除了这个 YAML,我知道足够的 Java 来做我正在做的事情。我也没有太多阅读文档或其他人代码的经验。
    • 您应该养成阅读文档的习惯:它可以为您省去(看似)随机错误的麻烦。至于 HashMap,请问你的困惑的根源是什么?
    猜你喜欢
    • 2013-09-18
    • 2016-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-26
    • 1970-01-01
    • 2017-07-29
    • 2016-11-10
    相关资源
    最近更新 更多