<i id='8gB72'><tr id='8gB72'><dt id='8gB72'><q id='8gB72'><span id='8gB72'><b id='8gB72'><form id='8gB72'><ins id='8gB72'></ins><ul id='8gB72'></ul><sub id='8gB72'></sub></form><legend id='8gB72'></legend><bdo id='8gB72'><pre id='8gB72'><center id='8gB72'></center></pre></bdo></b><th id='8gB72'></th></span></q></dt></tr></i><div id='8gB72'><tfoot id='8gB72'></tfoot><dl id='8gB72'><fieldset id='8gB72'></fieldset></dl></div>

      <tfoot id='8gB72'></tfoot>

      <small id='8gB72'></small><noframes id='8gB72'>

        <bdo id='8gB72'></bdo><ul id='8gB72'></ul>
    1. <legend id='8gB72'><style id='8gB72'><dir id='8gB72'><q id='8gB72'></q></dir></style></legend>

      Snakeyaml 似乎不必要地将简单值包装在列表中

      时间:2023-09-26
        <i id='m3VrM'><tr id='m3VrM'><dt id='m3VrM'><q id='m3VrM'><span id='m3VrM'><b id='m3VrM'><form id='m3VrM'><ins id='m3VrM'></ins><ul id='m3VrM'></ul><sub id='m3VrM'></sub></form><legend id='m3VrM'></legend><bdo id='m3VrM'><pre id='m3VrM'><center id='m3VrM'></center></pre></bdo></b><th id='m3VrM'></th></span></q></dt></tr></i><div id='m3VrM'><tfoot id='m3VrM'></tfoot><dl id='m3VrM'><fieldset id='m3VrM'></fieldset></dl></div>
      • <tfoot id='m3VrM'></tfoot>
          <bdo id='m3VrM'></bdo><ul id='m3VrM'></ul>

        • <legend id='m3VrM'><style id='m3VrM'><dir id='m3VrM'><q id='m3VrM'></q></dir></style></legend>

          <small id='m3VrM'></small><noframes id='m3VrM'>

                  <tbody id='m3VrM'></tbody>
                本文介绍了Snakeyaml 似乎不必要地将简单值包装在列表中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                我正在尝试使用 Groovy 和 Snakeyaml 解析以下 YAML 文件(显然我已经清理了数据,但足以证明问题):

                I am attempting to parse the following YAML file using Groovy and Snakeyaml (clearly I have sanitised the data but it is sufficient to demonstrate the issue):

                ---
                info:
                  summary: Snakeyaml Issue
                examples:
                  - 1st example:
                      name: Example 1
                      sublist:
                        - 0.1:
                           foo: bar
                

                我希望得到以下陈述:

                println resource.info.summary
                println resource.examples."1st example".name
                println resource.examples."1st example".sublist."0.1"
                

                屈服:

                Snakeyaml Issue
                Example 1
                [foo:bar]
                

                和:

                println resource.examples."1st example".sublist."0.1".foo
                

                屈服:

                bar
                

                但是,实际输出来自:

                println resource.info.summary
                println resource.examples."1st example".name
                println resource.examples."1st example".sublist."0.1"
                println resource.examples."1st example".sublist."0.1".foo
                

                是:

                Snakeyaml Issue
                [Example 1]
                [[[foo:bar]]]
                [[bar]]
                

                我只能通过包含列表索引来获得所需的输出:

                I can only get the desired output by including the list indicies:

                println resource.info.summary
                println resource.examples[0]."1st example".name
                println resource.examples[0]."1st example".sublist[0]."0.1"
                println resource.examples[0]."1st example".sublist[0]."0.1".foo
                

                这似乎是不必要的.也许我误解了数据的结构?

                which seems unnecessary. Perhaps I am misunderstanding the structure of the data?

                为了完整起见,我用来说明问题的常规代码如下所示(我从网上抓取了 CustomerResolver 代码,以便将浮点数保持为字符串):

                For completeness, the groovy code that I am using to illustrate the issue is shown below (I grabbed the CustomerResolver code off the web so that it would keep floats as strings):

                import org.yaml.snakeyaml.DumperOptions
                import org.yaml.snakeyaml.Yaml
                import org.yaml.snakeyaml.constructor.Constructor
                import org.yaml.snakeyaml.nodes.Tag
                import org.yaml.snakeyaml.representer.Representer
                import org.yaml.snakeyaml.resolver.Resolver
                
                def fileName = "example.yaml"
                def Yaml yaml = new Yaml(new Constructor(), new Representer(), new DumperOptions(),
                    new CustomResolver())
                def resource = yaml.load(new File(fileName).newInputStream())
                
                println resource.info.summary
                println resource.examples."1st example".name
                println resource.examples."1st example".sublist."0.1"
                println resource.examples."1st example".sublist."0.1".foo
                
                class CustomResolver extends Resolver {
                
                    /*
                     * Do not resolve float and timestamp
                     */
                
                    protected void addImplicitResolvers() {
                        addImplicitResolver(Tag.BOOL, BOOL, "yYnNtTfFoO");
                        addImplicitResolver(Tag.INT, INT, "-+0123456789");
                        addImplicitResolver(Tag.MERGE, MERGE, "<");
                        addImplicitResolver(Tag.NULL, NULL, "~nN");
                        addImplicitResolver(Tag.NULL, EMPTY, null);
                
                    }
                }
                

                有什么想法吗?

                推荐答案

                你如何访问yaml的问题

                the problem in a way how you access the yaml

                examples 首先包含 list,然后才包含 object 键为 1st example

                the examples first contains list and only then contains object with key 1st example

                对于您的情况,请尝试以下访问:

                for your case try this access:

                println resource.info.summary
                println resource.examples[0]."1st example"
                println resource.examples[0]."1st example".name
                println resource.examples[0]."1st example".sublist[0]
                println resource.examples[0]."1st example".sublist[0]."0.1"
                println resource.examples[0]."1st example".sublist[0]."0.1".foo
                

                要了解列表访问器的工作原理,请查看以下示例:

                to understand how list accessor works check this example:

                @Grab(group='org.yaml', module='snakeyaml', version='1.18')
                import org.yaml.snakeyaml.Yaml
                
                def Yaml yaml = new Yaml()
                def resource = yaml.load(new StringReader('''---
                info:
                  summary: Snakeyaml Issue
                examples:
                  - 1st example:
                      name: Example 1
                  - 1st example:
                      name: Example 2
                  - 1st example:
                      xname: Example 3
                  - 2nd example:
                      name: Example 4
                '''))
                
                println resource.examples."1st example"
                //prints>  [[name:Example 1], [name:Example 2], [xname:Example 3], null]
                
                println resource.examples."1st example".name
                //prints> [Example 1, Example 2, null]
                

                这篇关于Snakeyaml 似乎不必要地将简单值包装在列表中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:Jackson YAML:使用标志映射正则表达式模式 下一篇:使用 SnakeYaml 转储带引号的值

                相关文章

                最新文章

                1. <legend id='IwNby'><style id='IwNby'><dir id='IwNby'><q id='IwNby'></q></dir></style></legend>
                    <bdo id='IwNby'></bdo><ul id='IwNby'></ul>
                  1. <i id='IwNby'><tr id='IwNby'><dt id='IwNby'><q id='IwNby'><span id='IwNby'><b id='IwNby'><form id='IwNby'><ins id='IwNby'></ins><ul id='IwNby'></ul><sub id='IwNby'></sub></form><legend id='IwNby'></legend><bdo id='IwNby'><pre id='IwNby'><center id='IwNby'></center></pre></bdo></b><th id='IwNby'></th></span></q></dt></tr></i><div id='IwNby'><tfoot id='IwNby'></tfoot><dl id='IwNby'><fieldset id='IwNby'></fieldset></dl></div>
                  2. <small id='IwNby'></small><noframes id='IwNby'>

                    <tfoot id='IwNby'></tfoot>