Go语言加载无法处理YAML文件。

huangapple go评论97阅读模式
英文:

go Lang load unable to process yaml file

问题

我正在尝试读取一个 YAML 文件并将其存储在一个变量中,但由于某种原因,YAML 文件中的数组对象无法解组该文件,显示为空数据。

以下是我的 YAML 文件内容:

  1. ---
  2. version: "1.2"
  3. bUnits:
  4. - name: buname
  5. buGroupID: asd
  6. buStGroupID: asd
  7. - name: buname2
  8. buGroupID: asd2
  9. buStGroupID: asd2

以下是我使用的代码:

  1. type SResponse struct {
  2. Version string `json:"version"`
  3. BUnits []BUnit `json:"bUnits"`
  4. }
  5. type BUnit struct {
  6. Name string `json:"name"`
  7. BuUnitGroupID string `json:"buGroupID"`
  8. BuUnitStGroupID string `json:"buStaticGroupID"`
  9. }
  10. func main() {
  11. _printf := fmt.Printf
  12. _printf("Start")
  13. var sListResponse SResponse
  14. source, err2 := ioutil.ReadFile("squads2.yml")
  15. if err2 != nil {
  16. _printf("Couldn't read yaml file.")
  17. }
  18. err2 = yaml.Unmarshal(source, &sListResponse)
  19. if err2 != nil {
  20. _printf("Error")
  21. }
  22. _printf("Output: %s\n", sListResponse)
  23. }

代码可以读取版本部分,但是 bUnits 数组为空。请提供建议。

英文:

I am trying to read a yaml file and store it in a variable but for some reason the array object in YAML file is unable to unmarshall the file. it shows blank data.

Below is my yaml file content

  1. ---
  2. version: "1.2"
  3. bUnits:
  4. - name: buname
  5. buGroupID: asd
  6. buStGroupID: asd
  7. - name: buname2
  8. buGroupID: asd2
  9. buStGroupID: asd2

Below is the code that am using

  1. type SResponse struct {
  2. Version string `json:"version"`
  3. BUnits []BUnit `json:"bUnits"`
  4. }
  5. type BUnit struct {
  6. Name string `json:"name"`
  7. BuUnitGroupID string `json:"buGroupID"`
  8. BuUnitStGroupID string `json:"buStaticGroupID"`
  9. }
  10. func main() {
  11. _printf := fmt.Printf
  12. _printf("Start")
  13. var sListResponse SResponse
  14. source, err2 := ioutil.ReadFile("squads2.yml")
  15. if err2 != nil {
  16. _printf("Couldn't read yaml file.")
  17. }
  18. err2 = yaml.Unmarshal(source, &sListResponse)
  19. if err2 != nil {
  20. _printf("Error")
  21. }
  22. _printf("Output: %s\n", sListResponse)
  23. }

The code reads version part but the bUnits array comes empty.Please suggest.

答案1

得分: 0

你的结构体使用了json标签,这可能是因为你之后需要将数据导出/导入为JSON格式。但是你目前遇到的问题是yaml导入,所以你需要在结构体定义中添加yaml标签。

为了同时支持jsonyaml的编组/解组,你可以简单地更新你的标签,像这样:

  1. type SResponse struct {
  2. Version string `json:"version" yaml:"version"`
  3. BUnits []BUnit `json:"bUnits" yaml:"bUnits"`
  4. }
  5. type BUnit struct {
  6. Name string `json:"name" yaml:"name"`
  7. BuUnitGroupID string `json:"buGroupID" yaml:"buGroupID"`
  8. BuUnitStGroupID string `json:"buStaticGroupID" yaml:"buStaticGroupID"`
  9. }

如果你不需要进行JSON编码/解码,也可以直接删除json标签。

英文:

Your structs are decorated with json tags - which you may or may not need (depending on if you are exporting/import this data in JSON later).
But your problem at hand is with yaml importing - so you need to decorate your struct definitions with yaml tags.

To support both json and yaml marshaling/unmarshaling, simply update your tags like so:

  1. type SResponse struct {
  2. Version string `json:"version" yaml:"version"`
  3. BUnits []BUnit `json:"bUnits" yaml:"bUnits"`
  4. }
  5. type BUnit struct {
  6. Name string `json:"name" yaml:"name"`
  7. BuUnitGroupID string `json:"buGroupID" yaml:"buGroupID"`
  8. BuUnitStGroupID string `json:"buStaticGroupID" yaml:"buStaticGroupID"`
  9. }

Or just drop the json tags if you have no need for JSON encoding/decoding.

答案2

得分: 0

你应该在你的结构体中使用yaml标签。

  1. type SResponse struct {
  2. Version string `yaml:"version"`
  3. BUnits []BUnit `yaml:"bUnits"`
  4. }
  5. type BUnit struct {
  6. Name string `yaml:"name"`
  7. BuUnitGroupID string `yaml:"buGroupID"`
  8. BuUnitStGroupID string `yaml:"buStaticGroupID"`
  9. }

这样,你的结构体字段就可以正确地与 YAML 数据进行映射了。

英文:

you should use yaml tag in your struct

  1. type SResponse struct {
  2. Version string `yaml:"version"`
  3. BUnits []BUnit `yaml:"bUnits"`
  4. }
  5. type BUnit struct {
  6. Name string `yaml:"name"`
  7. BuUnitGroupID string `yaml:"buGroupID"`
  8. BuUnitStGroupID string `yaml:"buStaticGroupID"`
  9. }

huangapple
  • 本文由 发表于 2023年3月24日 20:10:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/75833485.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定