用密钥解析Yaml提供错误

时间:2019-03-05 14:16:30

标签: go yaml

我需要解析以下Yaml, 我尝试了以下


Build-t:
  before: test1
    - value : dddd 
       -  bbb: zzzz

  after: test2
     - value: bbb
        - aaa: aaaa


我尝试了以下操作:

type root struct{
 build type Build `yaml:"Build-t,omitempty"`
} 


type Build struct {
    Before map[string]interface{} `yaml:"before,omitempty"`
    After map[string]interface{} `yaml:"after,omitempty"`
 }

现在,当我解析它时出现错误,

我需要从对象beforeafter获取值,它们是yaml中的硬编码值 可以动态添加它下面的所有其他值,因此我将其设置为interface

顺便说一句,如果我将根更改为它的正常工作,并且我看到Build-t下的所有字段,但before and after就像键...

type root struct{
 build type map[string]interface{} `yaml:"Build-t,omitempty"`
} 

错误是:

line 6: cannot unmarshal !!str `test1` into map[string]interface {}
        line 7: cannot unmarshal !!str `test2` into map[string]interface {}

在此查看有效的Yaml https://codebeautify.org/yaml-validator/cb705458

1 个答案:

答案 0 :(得分:2)

这听起来是正确的-YAML无效。你的意思是这样吗?

Build-t:
 before:
   test1:
     - value: dddd
     - bbb: zzzz

 after:
   test2:
     - value: bbb
     - aaa: aaaa

请记住,空格很重要,并且它是键值结构-因此您的值可以是字符串或子结构-不能同时使用。

另外,那个Yaml验证器...我似乎无法使它声明任何无效内容!