我有以下需要解析的 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"` }現在當我解析它時出現錯誤,我需要的是從對象中獲取值before,after這些值是 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 回答

阿波羅的戰車
TA貢獻1862條經驗 獲得超6個贊
聽起來不錯——YAML 無效。你的意思是這樣的嗎?
Build-t:
before:
test1:
- value: dddd
- bbb: zzzz
after:
test2:
- value: bbb
- aaa: aaaa
請記住,空格很重要,它是一個鍵值結構——所以你的值可以是字符串,也可以是子結構——不能兩者都是。
另外,那個 yaml 驗證器......我似乎無法讓它聲明任何東西無效!
- 1 回答
- 0 關注
- 239 瀏覽
添加回答
舉報
0/150
提交
取消