duanlu1950 2019-03-05 06:16
浏览 550
已采纳

用密钥解析Yaml提供错误

I’ve the following yaml which I need to parse, I’ve tried with the following


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

  after: test2
     - value: bbb
        - aaa: aaaa


I’ve tried with the following:

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"`
 }

Now when I parse it I got error,

What I need is to get the values from the object before and after which are hardcoded value in the yaml And all the others value under it are dynmically can be added therefor I put it as interface

btw, if I change the root to this its working and I see all the fields under the Build-t but the before and after are like keys ...

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

the error is:

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

see the yaml valid here https://codebeautify.org/yaml-validator/cb705458

展开全部

  • 写回答

1条回答 默认 最新

  • douhuan3420 2019-03-05 06:30
    关注

    That sounds correct - the YAML is invalid. Did you mean something like this?

    Build-t:
     before:
       test1:
         - value: dddd
         - bbb: zzzz
    
     after:
       test2:
         - value: bbb
         - aaa: aaaa
    

    Remember that the whitespace is important, and it's a key-value structure - so your values can either be strings, or substructures - not both.

    Also, that yaml validator... I can't seem to make it declare anything as invalid!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

悬赏问题

  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部