gpt4 book ai didi

json - 具有嵌套属性的 Golang YAML 到 JSON

转载 作者:IT王子 更新时间:2023-10-29 02:09:49 25 4
gpt4 key购买 nike

我正在尝试从文件中解析一些 YAML,并将每个项目转换为平面 JSON 对象。

我在处理包含多个/嵌套对象的数据集时遇到了一些问题。

  • 我无法解码每个文件的多个项目。
  • 我不确定如何获取嵌套属性,例如 item -> description -> en

当我运行以下代码时出现错误。

cannot unmarshal !!map into []item.Item

我认为我必须查看我的结构中的子类型,但我似乎无法使任何工作正常进行。

如有任何帮助,我们将不胜感激。


代码:

import (
"gopkg.in/yaml.v2"
"io/ioutil"
)

func parseItemYaml() {
filePath := "./typeIDs.yaml"

yamlFile, err := ioutil.ReadFile(filePath)
if err != nil {
log.Printf("yamlFile.Get err #%v ", err)
}

itemData := map[int][]Item{}

err = yaml.Unmarshal(yamlFile, &itemData)
if err != nil {
panic(err)
}

// Then convert to JSON
// Eventual persistance
}

项目结构:

type Item struct {
ID int
Name string `yaml:"name"`
Description string `yaml:"description"`
GroupID int `yaml:"groupID"`
IconID int `yaml:"iconID"`
GraphicID int `yaml:"graphicID"`
MarketGroupID int `yaml:"marketGroupID"`
Mass float64 `yaml:"mass"`
Published bool `yaml:"published"`
Volume float64 `yaml:"volume"`
}

所需的 JSON 格式:

{
"ID":1563,
"Description":"Radiates an omnidirectional pulse from the ship that causes EM damage to surrounding vessels.",
"GroupID":72,
"IconID":112,
"GraphicID":72,
"MarketGroupID":382,
"Mass":10.0,
"Published":true,
"Volume":12.5
},
{...},
{...}

数据子集:

1563:
description:
de: Ein ungerichteter Rundum-Impuls der vom Schiff ausgestrahlt wird und bei
umliegenden Schiffen EM-Schaden verursacht.
en: Radiates an omnidirectional pulse from the ship that causes EM damage
to surrounding vessels.
fr: Émet une impulsion omnidirectionelle qui part du vaisseau et provoque
des dégâts électromagnétiques à tous les vaisseaux situés à proximité.
ja: 全方位にパルスを放射し、周囲の艦にEMダメージを与える。
ru: Испускает сильный всенаправленный импульс, наносящий электромагнитный
ущерб всем объектам, окружающим корабль.
zh: 从舰船上发射一个全向脉冲,对周围船只造成电磁伤害。
graphicID: 2032
groupID: 72
iconID: 112
marketGroupID: 382
mass: 10.0
name:
de: Small EMP Smartbomb I
en: Small EMP Smartbomb I
fr: Petite bombe de proximité à IEM I
ja: 小型EMPスマートボムI
ru: Small EMP Smartbomb I
zh: 小型EMP立体炸弹 I
portionSize: 1
published: true
radius: 1000.0
volume: 12.5
1564:
basePrice: 650000.0
groupID: 152
iconID: 112
marketGroupID: 341
name:
de: Small EMP Smartbomb I Blueprint
en: Small EMP Smartbomb I Blueprint
fr: Plan de construction Petite bombe de proximité à IEM I
ja: 小型EMPスマートボムIブループリント
ru: Small EMP Smartbomb I Blueprint
zh: 小型EMP立体炸弹蓝图 I
portionSize: 1
published: true
volume: 0.01
1565:
description:
de: Ein ungerichteter Rundum-Impuls der vom Schiff ausgestrahlt wird und bei
umliegenden Schiffen EM-Schaden verursacht.
en: Radiates an omnidirectional pulse from the ship that causes EM damage
to surrounding vessels.
fr: Émet une impulsion omnidirectionelle qui part du vaisseau et provoque
des dégâts électromagnétiques à tous les vaisseaux situés à proximité.
ja: 全方位にパルスを放射し、周囲の艦にEMダメージを与える。
ru: Испускает сильный всенаправленный импульс, наносящий электромагнитный
ущерб всем объектам, окружающим корабль.
zh: 从舰船上发射一个全向脉冲,对周围船只造成电磁伤害。
graphicID: 2032
groupID: 72
iconID: 112
marketGroupID: 382
mass: 40.0
name:
de: Small EMP Smartbomb II
en: Small EMP Smartbomb II
fr: Petite bombe de proximité à IEM II
ja: 小型EMPスマートボムII
ru: Small EMP Smartbomb II
zh: 小型EMP立体炸弹 II
portionSize: 1
published: true
radius: 1000.0
volume: 5.0

最佳答案

制作结构,例如像这样:

type Language struct {
De string `yaml:de`
En string `yaml:en`
Fr string `yaml:fr`
Ja string `yaml:ja`
Ru string `yaml:ru`
Zh string `yaml:zh`
}

type Item struct {
Description Language `yaml:description`
GroupID int `yaml:"groupID"`
IconID int `yaml:"iconID"`
GraphicID int `yaml:"graphicID"`
MarketGroupID int `yaml:"marketGroupID"`
Mass float64 `yaml:"mass"`
Name Language `yaml:name`
Published bool `yaml:"published"`
Volume float64 `yaml:"volume"`
}

此外,据我所知,它应该是 map[int]Item{} 而不是 map[int][]Item{}

关于json - 具有嵌套属性的 Golang YAML 到 JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50462026/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com